Because python is a dynamic language, there are many things that other languages can't do, and this is also mentioned in the documentation: 9.7. Odds and Ends.
We can define a class inside a function, and even create a class by using condition (if-else). Of course, you can also create functions by condition.
def getClass(x):
if x == 1:
class ClassA:
x = 1
return ClassA
else:
class ClassB:
x = 0
return ClassB
cls = getClass(1)
print(cls) # <class '__main__.getClass.<locals>.ClassA'>
print(cls.x) # 1
We can also create classes in a loop and keep the last class created when we leave.
for i in range(10):
class ClassA:
x = i
cls = ClassA()
print(cls.x) # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
print(cls.x) # 9
We can define class variables and instance variables in class early, and then use them outside when they are ready.
class Example:
def hi(self):
print(Example.cls_var)
print(self.inst_var)
e = Example()
Example.cls_var = "ya"
e.inst_var = "wow"
e.hi() # ya, wow
We can implement class variables
after defining class.
class Example:
pass
Example.cls_a = 1
e = Example()
print(e.cls_a) # 1
We can implement instance variables
after defining class.
class Example:
pass
e = Example()
e.msg = "wow"
e.hi = lambda self: print(self.msg)
e.hi(e) # wow