Changing static class variables in Python
Question:
How is it possible to change static variables of a class? I want it to be changed by some sort of input.
class MyClass:
var1 = 1
var2 = 4
def __init__(self, var3, var4):
self.var3 = var3
self.var4 = var4
It is var1 og var2 that i want to be changable, or want to know how to change.
Answers:
If you want to change the class variables, you can assign them to the variables in the __init__
scope:
class MyClass:
var1 = 1
var2 = 4
def __init__(self, var3, var4):
self.var1 = var3
self.var2 = var4
c = MyClass(100, 200)
>>>c.var1, c.var2
Output:
(100, 200)
It depends when you need to rebind the class attributes. You can also do so later when the object is created:
mc = MyClass(1, 2)
mc.var1 = 20
mc.var2 = 40
There is no difference between an attribute created or changed within the class body to one created outside by assigning to an attribute.
class Whatever():
b = 5
def __init__(self):
Whatever.b = 9999
boo = Whatever()
print(boo.b) # prints 9999
boo.b = 500
print(boo.b) # prints 500
Whatever.b = 400
print(boo.b) # prints 500
# since its a static var you can always access it through class name
# Whatever.b
You can change the class variables by class name as shown below:
class MyClass:
var1 = 1
var2 = 2
MyClass.var1 = 10 # By class name
MyClass.var2 = 20 # By class name
print(MyClass.var1) # Class variable
print(MyClass.var2) # Class variable
Output:
10
20
Be careful, if you try to change the class variables by object, you are actually adding new instance variables but not changing the class variables as shown below:
class MyClass:
var1 = 1
var2 = 2
obj = MyClass()
obj.var1 = 10 # Adding a new instance variable but not changing the class variable
obj.var2 = 20 # Adding a new instance variable but not changing the class variable
print(MyClass.var1) # Class variable
print(MyClass.var2) # Class variable
print(obj.var1) # New instance variable
print(obj.var2) # New instance variable
Output:
1
2
10
20
How is it possible to change static variables of a class? I want it to be changed by some sort of input.
class MyClass:
var1 = 1
var2 = 4
def __init__(self, var3, var4):
self.var3 = var3
self.var4 = var4
It is var1 og var2 that i want to be changable, or want to know how to change.
If you want to change the class variables, you can assign them to the variables in the __init__
scope:
class MyClass:
var1 = 1
var2 = 4
def __init__(self, var3, var4):
self.var1 = var3
self.var2 = var4
c = MyClass(100, 200)
>>>c.var1, c.var2
Output:
(100, 200)
It depends when you need to rebind the class attributes. You can also do so later when the object is created:
mc = MyClass(1, 2)
mc.var1 = 20
mc.var2 = 40
There is no difference between an attribute created or changed within the class body to one created outside by assigning to an attribute.
class Whatever():
b = 5
def __init__(self):
Whatever.b = 9999
boo = Whatever()
print(boo.b) # prints 9999
boo.b = 500
print(boo.b) # prints 500
Whatever.b = 400
print(boo.b) # prints 500
# since its a static var you can always access it through class name
# Whatever.b
You can change the class variables by class name as shown below:
class MyClass:
var1 = 1
var2 = 2
MyClass.var1 = 10 # By class name
MyClass.var2 = 20 # By class name
print(MyClass.var1) # Class variable
print(MyClass.var2) # Class variable
Output:
10
20
Be careful, if you try to change the class variables by object, you are actually adding new instance variables but not changing the class variables as shown below:
class MyClass:
var1 = 1
var2 = 2
obj = MyClass()
obj.var1 = 10 # Adding a new instance variable but not changing the class variable
obj.var2 = 20 # Adding a new instance variable but not changing the class variable
print(MyClass.var1) # Class variable
print(MyClass.var2) # Class variable
print(obj.var1) # New instance variable
print(obj.var2) # New instance variable
Output:
1
2
10
20