9.4, _ del _, _ doc _, _ dict _, _ module _, _ getitem _, _ setitem _, and __,__ delitem _, _ str _, _ call __, __del ____ doc __

Source: Internet
Author: User

9.4, _ del _, _ doc _, _ dict _, _ module _, _ getitem _, _ setitem _, and __,__ delitem _, _ str _, _ call __, __del ____ doc __
_ Del __:

  • Called destructor
  • When an object is deleted, the python interpreter calls the _ del _ () method by default.
Import gcclass Dog (object): def _ init _ (self, name): self. name = name def _ del _ (self): print ("deleted successfully") def wang (self): print ("wang ") d1 = Dog ("") d1.wang () # d1 = None # del d1 # both of the above can be deleted, or the memory will be released after the program ends # d1.wang () # An error is reported after deletion.

 

_ Doc __:
  • Class document string
Class Dog: "This is a description of this class" "def _ init _ (self, name): self. name = named1 = Dog ("Obama") # create an object and input the required Variable _ init _. By default, self automatically imports print (Dog. _ doc _) -------------------------- result: This is a description of this class.

 

_ Dict __:
  • Class attributes, including class-related variables and Methods
Class Dog: # self indicates the instance of the class, representing the address of the current object def _ init _ (self, name): # It is called a class constructor, initialize the member variable self. name = name def bulk (self): ### custom function print ("% s: Wang! "% Self. name) d1 = Dog ("Obama") # create an object and input the required Variable _ init _. By default, self automatically imports print (Dog. _ dict _) ### class-related variables, function print (d1. _ dict __) # Only instance variables --------------------------------------------- {'_ module _': '_ main _', '_ init _': <function Dog. _ init _ at 0x000002B6950FC9D8>, 'bulk': <function Dog. bulk at 0x000002B6950FCA60>, '_ dict _': <attribute '_ dict _' of 'Dog 'objects>, '_ weakref __': <attribute '_ weakref _' of 'Dog 'objects>, '_ doc _': None} {'name': 'Obama '}
_ Module __:
  • Module where the class definition is located: _ main __is displayed in the source file. When the class definition is imported as a module
_ Getitem __:
  • If the _ getitem _ method is defined in the class, its instance object (assumed as p) can be set to a value like p [key, when the Instance Object performs the p [key] operation, the method _ getitem _ in the class is called __.

_ Setitem __:
  • So that the object can use obj [key] = value to call _ setitem _ for the operation

_ Delitem __:
  • Enables the object to use del obj [key] to delete attributes.

 

 

Class A (object): def _ getitem _ (self, key): print ('_ getitem _', key) def _ setitem _ (self, key, value): self. value = value print ('_ setitem _', key, value) def _ delitem _ (self, key): print ('_ delitem __', key) obj = () result = obj ['k1 '] # Trigger _ getitem _ obj ['k2'] = 'neo' # Trigger _ setitem _ print (obj. value) # You can obtain the del obj ['k1 '] # Trigger _ delitem __

 

_ Str __:
  • The string that is displayed when the object is called directly.

 

class Dog(object):    def __init__(self,name):        self.name=name    def __str__(self):        return self.named=Dog("apple")print(d)
Class Dog (object): def _ init _ (self, name): self. name = name def _ str _ (self): return "Is this? "D = Dog (" apple ") print (d)

 

_ Call __:
  • A class instance can also be changed to a callable object. You only need to implement a special method _ call __().

 

class call_test(object):    def __call__(self, *args, **kwargs):        print("hello ",args,kwargs)c=call_test()c()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.