Syntax: Select all
def __setattr__(self, attr, value):
"""Find if the attribute is value and sets its value."""
# Is the given attribute private?
if attr.startswith('_'):
# Get the name of the private attribute
name = attr[1:]
# Is the attribute a property?
if (name in super(BaseEntity, self).__dir__() and isinstance(
getattr(self.__class__, name), property)):
# Set the private attribute's value
super(BaseEntity, self).__setattr__(attr, value)
# No need to go further
return
# If not a property, do not allow the private attribute
raise ValueError(
'Invalid private attribute "{0}" given.'.format(attr))
Please fix, or give us a good explanation!