type()
A = type("A", (object, ), dict(x="x")) print A().x print A.x A.x = "class" a = A() print a.x a.x = "instance" print a.x print A.x ## inheritance B = type("B", (A, ), dict(y="y")) print B.x, B.y # > x # > x # > class # > instance # > class # > class y
## classmethod, staticmethod, instance variable def __init__(self): self.x = 10 top = type("top", (object, ), {"static_fn" : staticmethod(lambda : "static top"), "class_fn" : classmethod(lambda cls : "class top %s" % cls.x), "x" : "x", "__init__" : __init__, "instance_fn" : lambda self : "instance %s" % self.x }) print top.static_fn(), top.class_fn() print top().static_fn(), top().class_fn(), top().instance_fn() # > static top class top x # > static top class top x instance 10
まー、だから何?という話だけど。