Practice content: Create classes dynamically using type
1 __author__='Orcsir'2 3 4 @classmethod5 defClass_method (CLS):6 Print("I am Class_method ...")7 8 9 @staticmethodTen defStatic_method (): One Print("I am Staticmethod ...") A - - def __init__(self, x, y): theself.x =x -SELF.Y =y - - + defFool (self): - Print("I am fool ....") + A at #type (name of the class, - #tuple of the parent class (for inheritance, can is empty), - #dictionary containing attributes names and values) - -Cls_name ="Spam" -bases =(object,) inDCT = {"__init__":__init__, - "Class_method": Class_method, to "Static_method": Static_method, + "fool": Fool} - the #Creating Spam class dynamically *Spam =type (cls_name, bases, DCT) $ Panax Notoginseng Spam.class_method () - Spam.static_method () the +s = Spam (1, 2) AS.fool ()
Basic knowledge of Python programming practice _004