I am messing around with the property descriptor in python, but an interesting error (at least for me) popped up (recursionerror).This is the code that caused it
class Employer: def __init__(self, age, name): self.age = age self.name = name def get_age(self): return self.age def set_age(self, value): if value > 30: raise ValueError("Our company doesn't want old people") self.age = value def deleter(self): del self.age age = property(get_age, set_age, deleter) emp = Employer(20, "Alex") Now, after I changed self.age in the setter method to self._age and did the same thing in the getter and deleter methods, everything worked fine, but I don’t really get why.I looked at the documentation but I don’t really understand this : "Name mangling is helpful for letting subclasses override methods without breaking intraclass method calls.” When I get the recursion error I reckon that something is being broken, but it doesn’t seem to me.Follow my reasoning:
- I define the attribute which is goint to be stored in a dictionary
- I modify that attribute using self.attribute_name I mean it seems that something is already running when i try to modify the attribute name using my approach.Could someone link the concept of private variables with the property descriptor, because probably that’s where the problem lies.Thank you for your attention.