[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why can't access the property setter using the super?

I have 3 classes which are connected to them through inheritance as you see in the below code:

import iso6346

class ShippingContainer:
    """docstring for ShippingContainer"""

    HEIGHT_FT = 8.5
    WIDTH_FT = 8.0
    next_serial = 1337

    def _get_next_serial(cls):
        result = cls.next_serial
        cls.next_serial += 1
        return result

    def _make_bic_code(owner_code, serial):
        return iso6346.create(owner_code=owner_code,

    def create_empty(cls, owner_code, length_ft, *args, **keyword_args):
        return cls(owner_code, length_ft, contents=None, *args, **keyword_args)

    # ... skipped

    def __init__(self, owner_code, length_ft, contents):
        self.contents  = contents
        self.length_ft = length_ft
        self.bic = self._make_bic_code(owner_code=owner_code,
        # ... skipped

class RefrigeratedShippingContainer(ShippingContainer):
    MAX_CELSIUS = 4.0

    def __init__(self, owner_code, length_ft, contents, celsius):
        super().__init__(owner_code, length_ft, contents)
        self.celsius = celsius

    # ... skipped

    def _make_bic_code(owner_code, serial):
        return iso6346.create(owner_code=owner_code,
    def celsius(self):
        return self._celsius

    def celsius(self, value):
        if value > RefrigeratedShippingContainer.MAX_CELSIUS:
            raise ValueError("Temperature too hot!")
        self._celsius = value

    # ... skipped

class HeatedRefrigeratedShippingContainer(RefrigeratedShippingContainer):
    MIN_CELSIUS = -20.0

    def celsius(self, value):
        if value < HeatedRefrigeratedShippingContainer.MIN_CELSIUS:
            raise ValueError("Temperature too cold!")
        super().celsius = value

Now when I run the code :

Python 3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018, 02:44:43) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from shipping import *                                        >>> h1 = HeatedRefrigeratedShippingContainer.create_empty('YML', length_ft=40, celsius=-18)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/aruprakshit/python_playground/shipping.py", line 23, in create_empty
    return cls(owner_code, length_ft, contents=None, *args, **keyword_args)
  File "/Users/aruprakshit/python_playground/shipping.py", line 47, in __init__
    self.celsius = celsius
  File "/Users/aruprakshit/python_playground/shipping.py", line 92, in celsius
    super().celsius = value
AttributeError: 'super' object has no attribute 'celsius'

Why here super is denying the fact that it has no celsius setter, although it has. While this code doesn?t work how can I solve this. The thing I am trying here not to duplicate the validation of temperature which is already in the  setter property of the RefrigeratedShippingContainer class.


Arup Rakshit
ar at zeit.io