How can I declare a static attribute in Python?
Here is written how I can declare a method: Static methods in Python?
How can I declare a static attribute in Python?
Here is written how I can declare a method: Static methods in Python?
All variables defined on the class level in Python are considered static
class Example:
Variable = 2 # static variable
print Example.Variable # prints 2 (static variable)
# Access through an instance
instance = Example()
print instance.Variable # still 2 (ordinary variable)
# Change within an instance
instance.Variable = 3 #(ordinary variable)
print instance.Variable # 3 (ordinary variable)
print Example.Variable # 2 (static variable)
# Change through Class
Example.Variable = 5 #(static variable)
print instance.Variable # 3 (ordinary variable)
print Example.Variable # 5 (static variable)
You can have two different variables in your class under the same name (one static and one ordinary). Don't be confused.
@staticmethod. The only answer I see that correctly identifies the difference between class attributes and staticattributes is the downvoted one (!)All variables declared inside the Class' body are 'static' attributes.
class SomeClass:
# this is a class attribute
some_attr = 1
def __init__(self):
# this is an instance attribute
self.new_attr = 2
But keep in mind that the 'static' part is by convention, not imposed (for more details on this, read this SO thread).
For more details of this convention and its implications, here is a quick excerpt from the official documentation:
“Private” instance variables that cannot be accessed except from inside an object, don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.
Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped. This mangling is done without regard to the syntactic position of the identifier, as long as it occurs within the definition of a class.
Static attributes are data attributes in Python. So that attributes assigned in class:
>>> class A(object):
... a = 1
>>> A.a
1
This is different from C++ and Java, where a static member can't be accessed using an instance:
>>> inst = A()
>>> inst.a
1
Also builtin method setattr will help you to set static variable(data attribute).
>>> setattr(A, 'b', 2)
>>> A.b
>>> inst.b
namedtuple that returns the attribute as a type and it worked for me. class main: globals: dict = sys.modiles['__main__'].__dict__From those that come here to use setattr as a means of adding static method to class, you need staticmethod function.
def func_c():
return 1
class A:
pass
setattr(A, 'f', staticmethod(func_c))
a_instance = A()
a_instance.f()
Rationale
If it is your class, it is easier to write it like this:
class A:
@staticmethod
def func_c():
return 1
But, that is not always the case.
Since default setattr when passing function changes to classmethod, this will throw TypeError: func_c() takes 0 positional arguments but 1 was given.
def func_c():
return 1
class A:
pass
setattr(A, 'f', func_c)
a_instance = A()
a_instance.f()
You can use standard @property decorator to make static attribute:
class A(object):
@property
def a(self):
return 1
a = A()
print a.a
1
a.a = 2
AttributeError: can't set attribute
static int statischeVariable=5;