I have the following code:
class Desc(object):
@property
def color_desc(self):
return 'Color is ' + self.color
@property
def brand_desc(self):
return 'Brand is ' + self.brand
class Text(Desc):
def __init__(self):
self.color = 'blue'
self.brand = 'volvo'
def main():
t = Text()
print t.color_desc
if __name__ == '__main__':
main()
This correctly works and outputs Color is blue
when run. However, if I modify the code slightly so that the property is set to an actual Class attribute, as in:
class Desc(object):
def __init__(self):
self._color_desc = color_desc
self._brand_desc = brand_desc
@property
def color_desc(self):
return self._color_desc
@color_desc.setter
def color_desc(self):
self._color_desc = 'Color is ' + self.color
@property
def brand_desc(self):
return self._brand_desc
@property
def brand_desc(self):
self._brand_desc = 'Brand is ' + self.brand
class Text(Desc):
def __init__(self):
self.color = 'blue'
self.brand = 'volvo'
All of a sudden, it errs out with AttributeError: 'Text' object has no attribute '_color_desc'
. How come the Text
attributes are inherited correctly in the first place but cannot be accessed in the second one. To me, these two solutions seem to do the same thing.