Object-oriented programming advancedStatic methods
Add Staticmethod After this function is still in the class, but it is not related to the class, and no need to call self
Direct D = Dog ("Chenronghua")
D.eat (d)
Example:Class Dog (object): Def __init__ (self, Name,): Self.name = name @staticmethod # This will cause an error when the Self def eat (self) is missing: Print ("%s is eating"% (self.name)) d = Dog ("Chen Ronghua") d.eat () typeerror:eat () Missing 1 required positional argument: ' Self
class MethodClass methods can only access analog variables
Property MethodTurn a method into a static property class Dog: Def __init__ (self, Name,): Self.name = name @property #attribute def Eat (self): print ("%s is eating"%self.name) d = Dog ("Chenronghua") d.eat
special member methods for classes
__doc__#直接打印这个类的描述class Foo (object): "" "This class is a description of the dog object" "" Print (foo.__doc__)
__module__ and __class____MODULE__ represents the current operation of the object in that module __class__ represents the current object's class is what
the __call__ object is appended with parentheses, triggering executionClass Dog (object): Def __cal__ (self, *args, *kwargs) d = Dog ("Chenronghua") d (1,2,3,name= "3333") __dict__ Print the property class D OG (object): Def __cal__ (self, *args, *kwargs) d = Dog ("Chenronghua") d (1,2,3,name= "3333") #打印类里的属性print dog.__dict__# Print Method in D print d.__dict__
ReflectionHasattr (OBJ,NAME_STR) getattr (obj_name_str) class Dog (object): def __init__ ( Self, name): Self.name = name def eat (self): Prin T ("%s is eating ..."%self.name) d = Dog ("niu") choice = input (">>:"). Strip () if Hasattr (d,choice): #判断对象里是否有对应的字符串的方法映射 func = GetAttr (d,choice) #根据字符串里的对象去获取对 Method memory address func ("Chenronghua") def Bulk (name): print ("%s da Jiao "%name) class Dog (object): def __init__ (self, name): Self.name = name def eat (Self,food): print ("%s eat%s"% (Self.name,food)) d = Dog ("Niuhanyang") # d.eat ("Chenronghua") def Bulk (self): print ("Yell%s"%self.name) choice = input (">& Gt : ") if hasattr (d,choice): PRInt (hasattr (d,choice)) GetAttr (D,choice) ("Grass") else: SetAttr (d,choice,bulk) GetAttr (D,choice) (d) #func = GETATTRT (d,choice) #func (d)
SOCKETCONN,ADDR = Server.accept () conn is a client connecting to the server for which an instance is generated addr is an IP address and port
Python_ Static Object _socket