简体   繁体   中英

Should I be using “global” or “self.” for class scope variables in Python?

Both of these blocks of code work. Is there a "right" way to do this?

class Stuff:
    def __init__(self, x = 0):
        global globx
        globx = x
    def inc(self):
        return globx + 1

myStuff = Stuff(3)
print myStuff.inc()

Prints "4"

class Stuff:
    def __init__(self, x = 0):
        self.x = x
    def inc(self):
        return self.x + 1

myStuff = Stuff(3)
print myStuff.inc()

Also prints "4"

I'm a noob, and I'm working with a lot of variables in a class. Started wondering why I was putting "self." in front of everything in sight.

Thanks for your help!

You should use the second way, then every instance has a separate x

If you use a global variable then you may find you get surprising results when you have more than one instance of Stuff as changing the value of one will affect all the others.

It's normal to have explicit self's all over your Python code. If you try tricks to avoid that you will be making your code difficult to read for other Python programmers (and potentially introducing extra bugs)

There are 2 ways for "class scope variables". One is to use self , this is called instance variable, each instance of a class has its own copy of instance variables; another one is to define variables in the class definition, this could be achieved by:

class Stuff:
  globx = 0
  def __init__(self, x = 0):
    Stuff.globx = x 
  ...

This is called class attribute, which could be accessed directly by Stuff.globx , and owned by the class, not the instances of the class, just like the static variables in Java.

you should never use global statement for a "class scope variable", because it is not. A variable declared as global is in the global scope, eg the namespace of the module in which the class is defined.

namespace and related concept is introduced in the Python tutorial here.

Those are very different semantically. self. means it's an instance variable, ie each instance has its own. This is propably the most common kind, but not the only one. And then there are class variables, defined at class level (and therefore by the time the class definition is executed) and accessable in class methods. The equivalent to most uses of static methods, and most propably what you want when you need to share stuff between instances (this is perfectly valid, although not automatically teh one and only way for a given problem). You propably want one of those, depending on what you're doing. Really, we can't read your mind and tell you which one fits your problem.

Globals variables are a different story. They're, well, global - everyone has the same one. This is almost never a good idea (for reasons explained on many occasions), but if you're just writing a quick and dirty script and need share something between several places, they can be acceptable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM