Variables declared inside the class definition, but not inside a method are class or static variables:

>>> class MyClass:
...     i = 3
...
>>> MyClass.i
3 

As @millerdev points out, this creates a class-level i variable, but this is distinct from any instance-level i variable, so you could have

>>> m = MyClass()
>>> m.i = 4
>>> MyClass.i, m.i
>>> (3, 4)

This is different from C++ and Java, but not so different from C#, where a static member can't be accessed using a reference to an instance.

See what the Python tutorial has to say on the subject of classes and class objects.

@Steve Johnson has already answered regarding static methods, also documented under "Built-in Functions" in the Python Library Reference.

class C:
    @staticmethod
    def f(arg1, arg2, ...): ...

@beidy recommends classmethods over staticmethod, as the method then receives the class type as the first argument.

Answer from Blair Conrad on Stack Overflow
Top answer
1 of 16
2410

Variables declared inside the class definition, but not inside a method are class or static variables:

>>> class MyClass:
...     i = 3
...
>>> MyClass.i
3 

As @millerdev points out, this creates a class-level i variable, but this is distinct from any instance-level i variable, so you could have

>>> m = MyClass()
>>> m.i = 4
>>> MyClass.i, m.i
>>> (3, 4)

This is different from C++ and Java, but not so different from C#, where a static member can't be accessed using a reference to an instance.

See what the Python tutorial has to say on the subject of classes and class objects.

@Steve Johnson has already answered regarding static methods, also documented under "Built-in Functions" in the Python Library Reference.

class C:
    @staticmethod
    def f(arg1, arg2, ...): ...

@beidy recommends classmethods over staticmethod, as the method then receives the class type as the first argument.

2 of 16
789

@Blair Conrad said static variables declared inside the class definition, but not inside a method are class or "static" variables:

>>> class Test(object):
...     i = 3
...
>>> Test.i
3

There are a few gotcha's here. Carrying on from the example above:

>>> t = Test()
>>> t.i     # "static" variable accessed via instance
3
>>> t.i = 5 # but if we assign to the instance ...
>>> Test.i  # we have not changed the "static" variable
3
>>> t.i     # we have overwritten Test.i on t by creating a new attribute t.i
5
>>> Test.i = 6 # to change the "static" variable we do it by assigning to the class
>>> t.i
5
>>> Test.i
6
>>> u = Test()
>>> u.i
6           # changes to t do not affect new instances of Test

# Namespaces are one honking great idea -- let's do more of those!
>>> Test.__dict__
{'i': 6, ...}
>>> t.__dict__
{'i': 5}
>>> u.__dict__
{}

Notice how the instance variable t.i got out of sync with the "static" class variable when the attribute i was set directly on t. This is because i was re-bound within the t namespace, which is distinct from the Test namespace. If you want to change the value of a "static" variable, you must change it within the scope (or object) where it was originally defined. I put "static" in quotes because Python does not really have static variables in the sense that C++ and Java do.

Although it doesn't say anything specific about static variables or methods, the Python tutorial has some relevant information on classes and class objects.

@Steve Johnson also answered regarding static methods, also documented under "Built-in Functions" in the Python Library Reference.

class Test(object):
    @staticmethod
    def f(arg1, arg2, ...):
        ...

@beid also mentioned classmethod, which is similar to staticmethod. A classmethod's first argument is the class object. Example:

class Test(object):
    i = 3 # class (or static) variable
    @classmethod
    def g(cls, arg):
        # here we can use 'cls' instead of the class name (Test)
        if arg > cls.i:
            cls.i = arg # would be the same as Test.i = arg1

🌐
FavTutor
favtutor.com › blogs › static-variable-python
Static Variable in Python (How to Create and Access it?)
September 12, 2022 - Python does not have a 'static' keyword to declare the static variable. In Python, you simply declare a variable inside the class, note that it should not be inside any method, and that variable will be known as a static or class variable.
Discussions

Can someone explain how object/instance variables vs class/static variables work in Python?
In the first example, you get an error because the attribute, var1, hasn't been defined before you use it in an expression. So, you would need to do this. class A: def __init__(self): pass def some_func(self): self.var1 += 1 a = A() a.var1 = 0 # create attribute a.some_func() print(a.var1) In your second example, as you've defined a class variable, that is used to define the instance variable the first time you use a local assignment expression in the instance method. Thus, in the below, you will find that a has an instance variable but b doesn't, class A: var1 = 0 def __init__(self): pass def some_func(self): self.var1 += 1 a = A() b = A() a.some_func() print(a.var1, b.var1) print(vars(a), vars(b)) # outputs all the attributes of the instances PS. If I modify the class variable, A.var1 += 10 then you would find b.var1 was also changed (because it is pointing to the same object). More on reddit.com
🌐 r/learnpython
17
39
July 6, 2023
What is the Python equivalent of static variables inside a function? - Stack Overflow
I don't know what @S.Lott means ... in class-variable position to me. 2015-10-20T17:05:05.977Z+00:00 ... Based on my research, this class technique appears to be the most "Pythonic" of the approaches presented on this page, and uses the least trickery. I therefore plan on adopting it as my go-to replacement for C-static-like variables ... More on stackoverflow.com
🌐 stackoverflow.com
AWS Lambda and static variables
Does anyone know how to set up a value to persist across multiple invocations of the lambda function? I'm using one to generate a text message to… More on reddit.com
🌐 r/aws
7
2
June 29, 2018
Python - How to how to reset class attributes?

I don't believe lazypenguin18 or Arxae are correct.

First of all, def attack(attack): is kind of a weird method declaration.

When you make a class, all methods (functions that are a part of the class) have the first parameter be the object in question. Essentially, every time you call a method of a class, you're calling the function and giving the object itself as an argument. For example, calling MONSTER.attack() is calling attack(Monster-object). This lets you access the attributes (variables that are a part of the class) of the object. The interpreter automatically inserts this for you, so even though the method declaration is def attack(self):, All you run is MONSTER.attack(). By using the period syntax, you are giving the self parameter its argument. Although there is nothing wrong with naming that parameter attack, it strikes me as unfamiliarity with how python classes work, but that's okay. It's a little obscure. To be specific, you're supposed to access attributes of an object by using whatever the first parameter is named (Normally this is named self for convention, but in this case you have it named attack). This sort of leads into my next point, which is what I think is causing your error.

In Python, this is how you're supposed to initialize attributes (I'm going to make a pretend player, just to stick with your theme):

class Player(object):
    def __init__(self): #This is the "constructor"
        self.gun = Gun() #Lets pretend Gun is another class

I have a feeling you have it written like this:

class Player(object):
    gun = Gun()

This works, but is not what you want. When you create attributes inside the class like that, not using the above (Specifically, you can do it anytime using the self reference, but __init__ is called when the object is created so it's handy to do it then), what you create are class attributes. These are variables that only exist once for all the objects that exist of this kind. For example, if we have a gun defined like the above (And not using the self reference), then if we create 5 Player objects, they would all be using the same gun. If we modify gun, perhaps player1.gun = 5, then player2.gun would also be 5, and so on.

If you were to use the first format (With the self.gun reference), then player1.gun = 5 would leave player2.gun unchanged from its original value. They would be separate, and unique to each object.

I believe what you're doing is you've initialized the Monster HP attribute as a class attribute, and this is remaining at 0 because it is being changed for the whole class, and not just that individual object.

With this in mind, you will have to change your code a bit. I notice this in this part, print "You attack with your", gun.title(), you will have to change gun.title() to attack.gun.title(), but it would be best if you renamed the attack parameter to self.

There are two other, big things I notice in your code, but I think I'm about to hit the character limit any moment. Sorry if this was a little long winded, if you'd like me to elaborate, explain, or even talk about those two other things, feel free to drop me a PM.

More on reddit.com
🌐 r/gamedev
4
0
August 30, 2012
🌐
GeeksforGeeks
geeksforgeeks.org › python › g-fact-34-class-or-static-variables-in-python
Class (Static) and Instance Variables in Python - GeeksforGeeks
Class variables are shared by all objects of a class, whereas instance variables are unique to each object. Unlike languages such as Java or C++, Python does not require a static keyword.
Published: March 6, 2026
🌐
Javatpoint
javatpoint.com › static-in-python
Static in Python - Javatpoint
Static in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
🌐
Wiingy
wiingy.com › home › learn › python › class or static variable in python
Class or Static Variable in Python
January 30, 2025 - Static variables can be accessed without creating an instance of the class, while instance variables can only be accessed through an instance of the class. Here are some examples of accessing static variables in Python:
🌐
pythontutorials
pythontutorials.net › blog › access-static-variable-from-static-method
How to Access a Static Variable from a Static Method in Python Without Passing the Class: Fixing NameErrors and OOP Best Practices — pythontutorials.net
Since static methods do not receive a reference to the class (cls) or instance (self), they cannot implicitly access the class’s namespace. Thus, Python cannot find static_var when directly referenced, leading to a NameError. To access a static variable from a static method, we need to explicitly reference the class’s namespace.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › can someone explain how object/instance variables vs class/static variables work in python?
r/learnpython on Reddit: Can someone explain how object/instance variables vs class/static variables work in Python?
July 6, 2023 -

So I come from a Java background where defining, declaring and accessing static and instance level variables are pretty much a straightforward process. I want to be able to understand OOP concepts of Python properly so I have been doing some practice.

I have a class:

class A:

def init(self): pass

def someFunc(self): self.var1 += 1

I create an object of this class and call the someFunc() method:

a = A() 
a.someFunc()

It gives me an error. Ok, fair enough since I haven't declared a self.var1 variable yet.

Consider another example.

class A:

var1 = 10

def init(self): pass

def someFunc(self): self.var1 += 1

Now when I do this:

a = A()
a.someFunc()

Output: 11

I know that variables defined just below the class definition are class/static variables. And to access them you have to do A.var1

But why does it not give me an error now? I haven't created a object/instance level self.var1 variable yet, just a class level variable var1.

And when I call A.var1 the output is 10. Why is the output not the same as a.var1?

Does python automatically use the class level variable with the same name since there is no instance level variable defined with the same name? And does that in turn become a different variable from the class level variable?

Can someone please elaborate?

Top answer
1 of 11
9
In the first example, you get an error because the attribute, var1, hasn't been defined before you use it in an expression. So, you would need to do this. class A: def __init__(self): pass def some_func(self): self.var1 += 1 a = A() a.var1 = 0 # create attribute a.some_func() print(a.var1) In your second example, as you've defined a class variable, that is used to define the instance variable the first time you use a local assignment expression in the instance method. Thus, in the below, you will find that a has an instance variable but b doesn't, class A: var1 = 0 def __init__(self): pass def some_func(self): self.var1 += 1 a = A() b = A() a.some_func() print(a.var1, b.var1) print(vars(a), vars(b)) # outputs all the attributes of the instances PS. If I modify the class variable, A.var1 += 10 then you would find b.var1 was also changed (because it is pointing to the same object).
2 of 11
5
In python classes a reference to self.varname first looks for an instance attribute varname. If found, it is used. But if not found the class variable varname is used, if it exists. When assigning to self.varname an instance attribute is always created or updated, leaving any class variable of the same name unchanged. You can see that in this code, based on your second example: class A: var1 = 10 # no need to use "pass" in __init__() def someFunc(self): self.var1 += 1 a = A() a.someFunc() print(f"{a.var1=}, {A.var1=}") Run that code and you will see the instance variable is 11 and the class variable remains unchanged at 10. It's good practice to always refer to class variables by A.var1 so you don't fall into the trap of thinking assigning to self.var1 changes the class variable of name var1.
🌐
Scientech Easy
scientecheasy.com › home › blog › static variable in python
Static Variable in Python - Scientech Easy
August 1, 2026 - In other words, a variable that is mutually shared by all the instances (objects) of a class is called static variable. It is also called class variable or class attribute in Python because it belongs to a class itself rather than any specific instance of the class.
🌐
Medium
samedesilva.medium.com › python-oop-part-05-static-methods-and-variables-ac583aef6288
Python OOP part-05 Static Methods and variables. | by Sameera De Silva | Medium
December 14, 2025 - Often used for utility functions that logically belong to a class but don’t need its data. In Python, you define a static method using the @staticmethod decorator.
🌐
Dive into Python
diveintopython.org › home › learn python programming › classes in python › static class and methods
Static Classes in Python - How to Call Static Methods, Use Static Variables
May 3, 2024 - Overall, static classes and static methods provide a way to represent functionality that doesn't depend on any instance or class variables, and can be used without creating an instance of a class.
🌐
Finxter
blog.finxter.com › home › learn python blog › static class variables and methods in python
Static Class Variables and Methods in Python - Be on the Right Side of Change
July 1, 2022 - They are simply created like any variable but inside the class definition and outside the “def” that creates methods. We’ve also seen some peculiarities that class attributes have in Python. On the other hand, we have seen how to implement “static methods”, which do not require any instance to be used, using the “@staticmethod” decorator before the “def” with which we start the definition of the method itself.
🌐
Medium
medium.com › @abhishekjainindore24 › navigating-the-world-of-static-methods-and-static-variables-in-python-8f3a36072fd3
Navigating the World of Static Methods and Static Variables in Python | by Abhishek Jain | Medium
February 6, 2024 - class C: @staticmethod def m1(): print("M1") Calculator.m2() # self.m3() @staticmethod def m2(): print("M2") def m3(self): print("M3") Calculator.m2() self.m4() def m4(self): print("M4") # Using the static method without creating an instance C.m1(); obj1 = C() obj1.m3() ''' Output: M1 M2 M3 M2 M4 ''' ... Join Medium for free to get updates from this writer. ... Definition: A static variable is a variable shared by all instances of a class.
🌐
Scaler
scaler.com › home › topics › what is a static variable in python?
What is a Static Variable in Python? - Scaler Topics
June 15, 2024 - Class or static variables can be referred through a by class, but not through an instance directly. Python doesn't require users to use the 'static' keyword before declaring static variables.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 33025 › static-variables-in-python
static variables in python [SOLVED] | DaniWeb
September 27, 2005 - ... Python does not have C/C++-style static locals, but in modern Python the cleanest equivalent is a closure with a nonlocal binding so the state lives alongside the function and is not global. def make_static_num(start=0): x = start def ...
🌐
GeeksforGeeks
geeksforgeeks.org › g-fact-34-class-or-static-variables-in-python
Class or Static Variables in Python - GeeksforGeeks
September 30, 2024 - Access Through Class: Static variables ... variables doesn’t change for every object. The Python approach is simple; it doesn't require a static keyword....
🌐
Radek
radek.io › posts › static-variables-and-methods-in-python
Static variables and methods in Python
July 21, 2011 - How to declare a data member or a method static in Python? Static means, that the member is on a class level rather on the instance level. Static variables exist only on class level and aren’t instantiated.
🌐
Sololearn
sololearn.com › en › Discuss › 3095686 › static-variable-in-python-class
Static variable in Python class | Sololearn: Learn to code for FREE!
October 19, 2022 - Since "K" is a static variable, then "d.K" will also have the same value (1) - that's how static referencing works. However, now that you have an instance of "NewClass" (variable "d"), you can edit the attributes of it without conflicting with the actual class ("NewClass").