If I create an object:
class eggs(object):
""" This wasn't needed """
def __setattr__(self, name, value):
print name, value
I can understand that if I do:
class eggs(object):
""" This wasn't needed """
def __setattr__(self, name, value):
print name, value
if __name__ == "__main__":
foo = eggs()
foo.bar = 5
print foo
I get:
bar 5
<__main__.eggs object at 0x8f8bb0c>
However when I do:
if __name__ == "__main__":
foo = eggs()
foo = 5
print foo
I get:
5
My question is what "magic" method is called when foo = 5
is called?
For example I do do:
class eggs(object):
""" This wasn't needed """
def __init__(self):
self.bacon = False
def __setattr__(self, name, value):
if not name == "bacon":
raise Exception("No way Hosay")
if value not in [True, False]:
raise Exception("It wasn't supposed to be like this")
super(eggs, self).__setattr__(name, value)
print "Variable became {0}".format(value)
if __name__ == "__main__":
foo = eggs()
foo.bacon = True
foo.bacon = 5 > 4
foo.bacon = True or False
# ETC
Which returns:
Variable became False
Variable became True
Variable became True
Variable became True
I want to do that without bacon.