Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding data members to Python classes from outside the function definition

Tags:

python

class

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?

like image 530
smilingbuddha Avatar asked Jul 21 '12 21:07

smilingbuddha


People also ask

How do you define member function outside class Python?

Python isn't like Java or C# and you can just have functions that aren't part of any class. If you want to group together functions you can just put them together in the same module, and you can nest modules inside packages. Only use classes when you need to create a new data type, not just to group functions together.

Can we define data members outside the class?

Member functions and static members can be defined outside their class declaration if they have already been declared, but not defined, in the class member list. Nonstatic data members are defined when an object of their class is created. The declaration of a static data member is not a definition.

How do you access a variable outside the function in Python?

Using the global statement If you want to assign a value to a name defined outside the function, then you have to tell Python that the name is not local, but it is global. We do this using the global statement. It is impossible to assign a value to a variable defined outside a function without the global statement.

Can we define a method outside the class how and why?

No its not possible. In java everything (objects) belongs to some class. So we can not declare function(method) outside a class.


2 Answers

Because this is Python, not C++ (or Java). Calling it a misfeature is a fundamental misunderstanding of how Python works.

In Python you don't declare variables, or attributes. There's no such thing as "a data member of the point class". Your i is just a class-level variable, but it would be the same wherever you associate that attribute with the class. You can dynamically add attributes to classes, instances, modules, whatever you like. That's what it is to be a dynamically typed language.

In fact, doing this like this is the only way to define instance variables. As I said, your i above is a class attribute, shared by all members of the class. The only way to get an instance-level variable is to "dynamically" add it, usually in the __init__ method but you can do it wherever you like.

like image 114
Daniel Roseman Avatar answered Sep 19 '22 23:09

Daniel Roseman


It is just a common thing from scripting languages. Python lets you do it, Ruby also, and in deed, you never had to pre-define your local variables. Why not also in your classes? Not only that, you can choose if the new inserted variables/functions affect only one object or all instances of that class.

People who are doing heavy things in TDD and UnitTesting love that misfeature. Actually I would even go as far as to say that C++ static typing does not reduce my programs bugs nearly as much as a language that ease my unit tests giving me exactly that.

However, if you are concerned, you can always use _____slots_____

https://stackoverflow.com/a/3603624/253098

like image 40
SystematicFrank Avatar answered Sep 19 '22 23:09

SystematicFrank