Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to use self variable than pass variable in a class? [closed]

Tags:

python

I used to be a c programmer, so we have to pass every variable as argument or pointer and not encouraged to define global variable.

I am going to use some variable in several functions in python.

Generally, which is better, pass the variable as an argument, or define a self variable when we get the value of the variables? Does python has any general rules about this?

Like this:

class A:
    def func2(self, var):
        print var

    def func1(self):
        var = 1
        self.func2(var)

class B:
    def func2(self):
        print self.var

    def func1(self):
        self.var = 1
        self.func2()

Which is better? A or B?

like image 398
dspjm Avatar asked Mar 22 '14 02:03

dspjm


People also ask

When should I use self in Python?

Use self when:you define an instance method, since it is passed automatically as the first parameter when the method is called; you reference a class or an instance attribute from inside an instance method; you want to refer to instance variables and methods from other instance methods.

Why is the self variable used inside a class?

By using the “self” we can access the attributes and methods of the class in python. It binds the attributes with the given arguments. The reason you need to use self. is because Python does not use the @ syntax to refer to instance attributes.

When should we use instance variable?

Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed. Instance variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object's state that must be present throughout the class.

What happens if you don't use self in Python?

If there was no self argument, the same class couldn't hold the information for both these objects. However, since the class is just a blueprint, self allows access to the attributes and methods of each object in python. This allows each object to have its own attributes and methods.


2 Answers

In Python, you have a lot of freedom to do what "makes sense". In this case, I would say that it depends on how you plan on using func2 and who will be accessing it. If func2 is only ever supposed to act upon self.var, then you should code it as such. If other objects are going to need to pass in different arguments to func2, then you should allow for it to be an argument. Of course, this all depends on the larger scope of what you're trying to do, but given your simple example, this makes sense.

Also, I'm confused about how your question relates to global variables. Member variables are not the same thing as global variables.

Edited to reflect updated post:

The difference between A and B in your example is that B persists the information about self.var, while A does not. If var needs to be persisted as part of the object's state, then you need to store it as part of self. I get the sense that your question might relate more to objects as a general concept than anything Python-specific.

like image 67
Mike Bell Avatar answered Oct 11 '22 18:10

Mike Bell


Of course it's better to design your program to use scope intelligently. The most obvious problem is that a mutation of a global variable can affect distant parts of code in ways that are difficult to trace, but in addition, garbage collection (reference counting, whatever) becomes effectively moot when your references live in long-lived scopes.

That said, Python has a global keyword, but it doesn't have globals in the same way c does. Python globals are module level, so they're namespaced with the module by default. The downstream programmer can bypass or alias this namespacing, but that's his/her problem. There are certainly cases where defining a module-level configuration value, pseudo-enum or -const makes sense.

Next, consider whether you need to maintain state: if the behavior of an object depends on it being aware of a certain value, make it a property. You can do that by attaching the value to self. Otherwise, just pass the value as an argument. (But then, if you have a lot of methods and no state, ask yourself if you really need a class, or should they just be module functions?)

like image 30
kojiro Avatar answered Oct 11 '22 18:10

kojiro