Yes. Classes are first-class objects, and the execution of a class statement can make use of function parameters.
def make_class(value):
class Foo:
class_variable = value
@classmethod
def printVariable(cls):
print(cls.class_variable)
return Foo
Foo3 = make_class(3)
Foo3.printVariable() # outputs 3
You can also call type directly.
def make_class(value):
def printVariable(cls):
print(cls.class_variable)
return type('Foo', (), {'class_variable': value, 'printVariable': classmethod(printVariable)})
You also can do this without any function:
v = 3
class Foo:
class_variable = v
@classmethod
def printVariable(cls):
print(cls.class_variable)
v = 9
Foo.printVariable() # outputs 3, not 9
When the class statement is executed, it uses the value of v at that time to initialize the class attribute class_variable; it does not save the name v and wait until you want to see the value of class_variable before looking up v.