I just started learning python and am very confused at why I am able to do this. Will someone please explain why this works:
class foo(): @property def hello(self): return "hello world" if __name__ == '__main__': cli = foo() cli.foo2 = 3 print cli.hello #prints hello world print cli.foo2 #prints 3 It doesn't make sense to me that I am able to create a new attribute and assign a value when it doesn't exist in the original class. Is there a way to prevent this behavior or can someone at least explain the logic behind this?
-edit- I am glad to know this is normal behavior for objects in python and I wasn't just doing something wrong. It just didn't feel right as I normally work in VBA and C#. From what others have said in answers below I thought adding slots would prevent anything from being added to the class in this way but maybe I didn't understand.
class foo(): @property def hello(self): return "hello world" __slots__ = [] if __name__ == '__main__': cli = foo() cli.foo2 = 3 print cli.hello #prints hello world print cli.foo2 #prints