Adding data members to Python classes from outside the function definition

Viewed 4926

I am quite new to Python and I cant seem to be able to understand this.Consider this simple Python code.

class point:
   i = 34


test = point()
test.y = 45

print test.y 

As you can see I instance of point called test but then did test.y = 45 when y is not a data member of the point class. No error was thrown and the y attribute seems to have been added to the class automatically.

Why did this happen? Isn't this a misfeature? Or am I missing something very basic. The same thing cannot be done with C++ and it would throw a compiler error. Any reason for this strange feature?

3 Answers
Related