###Special member methods of classes

###Special member methods of 
classes #__call__ Objects are followed by parentheses to trigger execution.
##Note: The execution of the constructor is triggered by the creation of the object, that is: object = class name (): and the execution of the __call__ method is triggered by the parentheses after the object, that is: object () or class () ()
class Dog ( object) : def __init__( self, name, addr) : self.name = name self.addr = addr def blak( self) : print( "%s has a nice voice..." % self .name) def __call__( self, *args, **kwargs) : print( args, kwargs) def __str__( self) : return "<obj:%s>" %








self.name


dl = Dog( "Caicai", "22")
dl( 1, 2, 3, 4, name = "Caicai")

###__dict__ View all members in a class or object
print(Dog. __dict__) ###View all object methods in a class
##Output: {'__module__': '__main__', '__init__': <function Dog.__init__ at 0x057F6C90>, 'blak': <function Dog.blak at 0x057F6C48>, ' __call__': <function Dog.__call__ at 0x057F6C00>, '__dict__': <attribute '__dict__' of 'Dog' objects>, '__weakref__': <attribute '__weakref__' of 'Dog' objects>, '__doc__': None}
print(dl.__dict__) ##Get the members of the current object
##Output: {'name': 'Caicai', 'addr': '22'}


###__str__If a class defines the __str__ method, then print the object When , the return value of the method is output by default.
print(dl)
##Output: <obj:Caicai>

###__getitem__/__setitem__/__delitem__
###For indexing operations/like dictionaries. The above means to get, set and delete data
class Foo ( object) : def __getitem__( self, key) : print( "__getitem__", key) def __setitem__( self, key, value) : print( "__setitem__", key, value) def __delitem__( self, key) : print( "__delitem__", key) obi = Foo()


















Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297744&siteId=291194637