If I have the following code:
class Foo(object):
bar = 1
def bah(self):
print(bar)
f = Foo()
f.bah()
It complains
NameError: global name 'bar' is not defined
How can I access class/static variable bar
within method bah
?
-
More information regarding Python and statics can be found here: stackoverflow.com/questions/68645/python-static-variablebedwyr– bedwyr2009年04月01日 21:57:04 +00:00Commented Apr 1, 2009 at 21:57
6 Answers 6
Instead of bar
use self.bar
or Foo.bar
. Assigning to Foo.bar
will create a static variable, and assigning to self.bar
will create an instance variable.
-
13Foo.bar will work, but self.bar creates an instance variable, not a static one.bedwyr– bedwyr2009年04月01日 21:55:34 +00:00Commented Apr 1, 2009 at 21:55
-
63bedwyr, "print self.bar" will not create any instance variables (although assigning to self.bar will).Constantin– Constantin2009年04月01日 22:14:27 +00:00Commented Apr 1, 2009 at 22:14
-
1@Constantin -- I didn't realize that, it's an interesting distinction. Thanks for the correction :-)bedwyr– bedwyr2009年04月03日 01:14:54 +00:00Commented Apr 3, 2009 at 1:14
-
2But if you don't intend for there to be an ivar, its clearer to use Foo.classmember.mk12– mk122009年09月13日 20:58:56 +00:00Commented Sep 13, 2009 at 20:58
-
3when using static variables, it's a good idea to read the gotchas from here: stackoverflow.com/questions/68645/… . @Constantin gives one of the many gotchas.Trevor Boyd Smith– Trevor Boyd Smith2017年03月21日 16:27:28 +00:00Commented Mar 21, 2017 at 16:27
Define class method:
class Foo(object):
bar = 1
@classmethod
def bah(cls):
print cls.bar
Now if bah()
has to be instance method (i.e. have access to self), you can still directly access the class variable.
class Foo(object):
bar = 1
def bah(self):
print self.bar
-
7Why not just Foo.bar, instead of self.__class__.bar?mk12– mk122009年09月13日 20:59:41 +00:00Commented Sep 13, 2009 at 20:59
-
23@Mk12: When you've got class inheritance, a "class variable"could be in a base class or a subclass. Which do you want to refer to? Depends on what you're trying to do. Foo.bar would always refer to an attribute of the specified class--which might be a base class or a subclass. self.__class__.bar would refer to whichever class the instance is a type of.Craig McQueen– Craig McQueen2009年12月11日 04:47:02 +00:00Commented Dec 11, 2009 at 4:47
-
10Now we have reached that unfortunate point when we become so aware of the details of the language that we can discriminate between two finely attuned use cases. It does indeed depend what you want to do, but many people won't attend to such subtleties.holdenweb– holdenweb2011年12月14日 10:19:52 +00:00Commented Dec 14, 2011 at 10:19
-
2BTW, this is a RARE usage of "class variable". Much more common to have it defined in a specific class, here Foo. This is useful information for some advanced programming situations. But almost certainly not what the original question wanted as an answer (anyone who needs THIS answer will already know how to do Foo.bar). +1 because I learned something from this.ToolmakerSteve– ToolmakerSteve2013年11月25日 17:05:19 +00:00Commented Nov 25, 2013 at 17:05
-
3I'm learning about when to use class variables and methods (as opposed to instance variables and methods). When you want to access a class variable in an instance method, is it necessary to use self.__class__.bar? I noticed that self.bar works even though bar is a class variable not an instance variable.Bill– Bill2015年03月29日 18:30:14 +00:00Commented Mar 29, 2015 at 18:30
As with all good examples, you've simplified what you're actually trying to do. This is good, but it is worth noting that python has a lot of flexibility when it comes to class versus instance variables. The same can be said of methods. For a good list of possibilities, I recommend reading Michael Fötsch' new-style classes introduction, especially sections 2 through 6.
One thing that takes a lot of work to remember when getting started is that python is not java. More than just a cliche. In java, an entire class is compiled, making the namespace resolution real simple: any variables declared outside a method (anywhere) are instance (or, if static, class) variables and are implicitly accessible within methods.
With python, the grand rule of thumb is that there are three namespaces that are searched, in order, for variables:
- The function/method
- The current module
- Builtins
{begin pedagogy}
There are limited exceptions to this. The main one that occurs to me is that, when a class definition is being loaded, the class definition is its own implicit namespace. But this lasts only as long as the module is being loaded, and is entirely bypassed when within a method. Thus:
>>> class A(object):
foo = 'foo'
bar = foo
>>> A.foo
'foo'
>>> A.bar
'foo'
but:
>>> class B(object):
foo = 'foo'
def get_foo():
return foo
bar = get_foo()
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
class B(object):
File "<pyshell#11>", line 5, in B
bar = get_foo()
File "<pyshell#11>", line 4, in get_foo
return foo
NameError: global name 'foo' is not defined
{end pedagogy}
In the end, the thing to remember is that you do have access to any of the variables you want to access, but probably not implicitly. If your goals are simple and straightforward, then going for Foo.bar or self.bar will probably be sufficient. If your example is getting more complicated, or you want to do fancy things like inheritance (you can inherit static/class methods!), or the idea of referring to the name of your class within the class itself seems wrong to you, check out the intro I linked.
-
IIRC, there's technically 3(+) namespaces searched -- function-local, module/global, and builtins. Nested scopes means that multiple local scopes may be searched, but that's one of the exceptional cases. (...)Jeff Shannon– Jeff Shannon2009年04月02日 08:52:44 +00:00Commented Apr 2, 2009 at 8:52
-
Also, I'd be careful saying 'main module', as it is the function's containing module that's searched, not the main module... And looking up the attribute from an instance ref is a different thing, but this answer does explain why you need the instance/class ref.Jeff Shannon– Jeff Shannon2009年04月02日 08:57:07 +00:00Commented Apr 2, 2009 at 8:57
class Foo(object):
bar = 1
def bah(self):
print Foo.bar
f = Foo()
f.bah()
bar
is your static variable and you can access it using Foo.bar
.
Basically, you need to qualify your static variable with Class name.
-
3Consider adding more information to expand on your answer, or consider editing other answers to add this detail.User1010– User10102021年01月05日 18:59:38 +00:00Commented Jan 5, 2021 at 18:59
You can access class variables by object and directly by class name from the outside or inside of class and basically, you should access class variables directly by class name because if there are the same name class and instance variables, the same name instance variable is prioritized while the same name instance variable is ignored when accessed by object. So, using class name is safer than using object to access class variables.
For example, you can access the class variable by object and directly by class name from the outside of the class as shown below:
class Person:
name = "John" # Class variable
obj = Person()
print(obj.name) # By object
print(Person.name) # By class name
Output:
John
John
But, if you add the same name instance variable as the class variable by object:
class Person:
name = "John" # Class variable
obj = Person()
obj.name = "Tom" # Adds the same name instance variable as class variable
print(obj.name) # By object
print(Person.name) # By class name
Or, if you add the same name instance variable as the class variable by self
in __init__()
:
class Person:
name = "John" # Class variable
def __init__(self, name):
self.name = name # Adds the same name instance variable as class variable
obj = Person("Tom")
print(obj.name) # By object
print(Person.name) # By class name
The same name instance variable is prioritized when accessed by object:
Tom # By object
John # By class name
And, you can also access the class variable by self
and directly by class name from the inside of the instance method as shown below:
class Person:
name = "John" # Class variable
def test(self): # Instance method
print(self.name) # By "self"
print(Person.name) # By class name
obj = Person()
obj.test()
Output:
John
John
But, if you add the same name instance variable as the class variable by object:
class Person:
name = "John" # Class variable
def test(self): # Instance method
print(self.name) # By "self"
print(Person.name) # By class name
obj = Person()
obj.name = "Tom" # Adds the same name instance variable as the class variable
obj.test()
Or, if you add the same name instance variable as the class variable by self
in __init__()
:
class Person:
name = "John" # Class variable
def __init__(self, name):
self.name = name # Adds the same name instance variable as the class variable
def test(self): # Instance method
print(self.name) # By "self"
print(Person.name) # Directly by class name
obj = Person("Tom")
obj.test()
The same name instance variable is prioritized when accessed by self
:
Tom # By "self"
John # By class name
Explore related questions
See similar questions with these tags.