Properties/methods on a class
Class variable
A class variable is a field/attribute of a class. The value is for the class itself, but can also be accessed via objects. This is called static field in JAVA/C++, but it is almost the same.
However, it can be modified from an object and the value is for that partcular object only. If modified for the class, then it is modified for the class (of course) but also for the objects of that class.
class Animal:
nbLegs = 4
a = Animal()
b = Animal()
print(a.nbLegs, b.nbLegs, Animal.nbLegs)
Animal.nbLegs = 5
print(a.nbLegs, b.nbLegs, Animal.nbLegs)
a.nbLegs = 4
print(a.nbLegs, b.nbLegs, Animal.nbLegs)
Animal.nbLegs = 3
print(a.nbLegs, b.nbLegs, Animal.nbLegs)
4 4 4
5 5 5
4 5 5
4 3 3
class Animal:
def __init__(self):
self.nbLegs = 4
a = Animal()
print(a.nbLegs)
Animal.nbLegs
4
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[5], line 7
5 a = Animal()
6 print(a.nbLegs)
----> 7 Animal.nbLegs
AttributeError: type object 'Animal' has no attribute 'nbLegs'
Quiz
What is the answer of that program?
class A:
list = []
a = A()
b = A()
a.list.append(1)
b.list
[1]
class A:
def __init__(self):
self.list = []
a = A()
b = A()
a.list.append(1)
b.list
[]
Static methods
Static methods are functions declared inside a class. They behave like functions (no special pameters like self
). This is usually used group functions under a class name. It used for utilility functions.
class Calculator:
@staticmethod
def sum(x, y):
return x + y
Calculator.sum(1, 2)
3
class Animal:
nbLegs = 4
@staticmethod
def nbLegsForHello(proportion):
return Animal.nbLegs * proportion
def speak(self):
print(f"I have {self.nbLegsForHello(2)}")
print(Animal.nbLegsForHello(0.5))
a = Animal()
print(a.nbLegsForHello(0.5))
a.speak()
2.0
2.0
I have 8
Class methods
A class method is a method that is attached to the class and takes the class itself as the first argument. Usually, they are used for providing alternative constructors.
class Cat():
def __init__(self, catAge):
self.age = catAge
@classmethod
def createByEqHumanAge(cls, humanAge):
return cls(humanAge / 7)
garfield = Cat.createByEqHumanAge(21)
garfield.age
3.0
Class methods have nice behavior when inheritance is involved.
class Cat():
def __init__(self, catAge):
self.age = catAge
def speak(self):
print("classical miaou")
@classmethod
def createByEqHumanAge(cls, humanAge):
print(cls)
return cls(humanAge / 7)
class AngoraCat(Cat):
def __init__(self, catAge):
print("feu d'artifice")
self.age = catAge
def speak(self):
print("mIaOu")
a = AngoraCat.createByEqHumanAge(21)
print(a.age)
a.speak()
<class '__main__.AngoraCat'>
feu d'artifice
3.0
mIaOu
type