I have defined a simple class. The suprising thing is: I can assign attributes that haven't been defined in the class definition.
- How is that possible?
- How can I prevent this from happening?
See here, what I mean, this is my simple class:
class Dog:
def __init__(self,name):
self.name=name
Of course I can now instanciate an object: dog = Dog('Fido')
Printing Print(dog.name) yields 'Fido'.
But now I just can assign to my object dog new attributes though I haven't included them in the class definition.
For example: dog.mood="happy" works. When I print Print(dog.mood), I get 'happy', that means it works. Why is this possible and how can I prevent assigning values to attributes like "mood" even though I haven't defined them in my class definition of dog?