Special Python object-oriented Member, python object-oriented
Call for special members of a class
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ class SpecialMembers: # class constructor def _ init _ (self ): print ("My Blog is Url: http://yw666.blog.51cto.com") # Object Construction Method def _ call _ (self): print ("My Name is: Yangwen ") # create an object and the constructor of the execution class obj = SpecialMembers () # constructor of the execution class obj () # constructor of the execution class, then in the construction method of the execution object SpecialMembers ()()
Output
Yangwen @ Darker :~ $ Python3/home/yangwen/documentation/Python_code/sublime/Week06/Day04/s2.pyMy Blog is Url: http://yw666.blog.51cto.comMy Name is: YangwenMy Blog is Url: http://yw666.blog.51cto.comMy Name is: Yangwen
Getitem, setitem, and delitem
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ class SpecialMembers: # When obj ['value'] is executed, the _ getitem _ method is automatically executed, and treat the value in the brackets as the value of _ getitem _ def _ getitem _ (self, item): print (item) def _ setitem _ (self, key, value): print (key, value) def _ delitem _ (self, key): print (key) # create an object obj = SpecialMembers () # autoexecute _ getitem _ method obj ['value'] # autoexecute _ setitem _ method obj ['k1 '] = "values" # autoexecute _ delitem _ method del obj ['key']
Output
Ansheng @ Darker :~ $ Python3/home/yangwen/documentation/Python_code/sublime/Week06/Day04/s2.pyvaluek1 valueskey
Special
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ class SpecialMembers: # When obj ['value'] is executed, the _ getitem _ method is automatically executed, the value in the brackets of the object is treated as the value of _ getitem _ def _ getitem _ (self, item): print (item, type (item ), "_ getitem _") def _ setitem _ (self, key, value): print (key, value) def _ delitem _ (self, key ): print (key) # create an object obj = SpecialMembers () # autoexecute _ getitem _ method obj [] # _ getslice _/_ getitem __# autoexecute _ setitem _ method obj [] = [11, 22, 33] # _ setslice _/_ setitem __# Automatic Execution _ delitem _ del obj [] # _ delslice _/_ delitem __
Output
Yangwen @ Darker :~ $ Python3/home/yangwen/documentation/Python_code/sublime/Week06/Day04/s2.pyslice (1, 3, None) <class 'slice '> _ getitem _ slice (1, 3, None) [11, 22, 33] slice (1, 3, None)
Dict
Obtains all members of a class or object.
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ class SpecialMembers: "class comment" "def _ init _ (self ): self. name = "Ansheng" self. blog = "http://yw666.blog.51cto.com" # Get members in the class print (SpecialMembers. _ dict _) # create an object obj = SpecialMembers () # obtain the member print (obj. _ dict __)
Output
Yangwen @ Darker :~ $ Python3/home/yangwen/documentation/Python_code/sublime/Week06/Day04/s2.py {'_ weakref __': <attribute '_ weakref _' of 'specialmembers 'objects>,' _ doc _ ':' \ n class comment \ n ', '_ module _': '_ main _', '_ dict _': <attribute '_ dict _' of 'specialmembers 'objects>, '_ init _': <function SpecialMembers. _ init _ at 0x7ff2af2d7598 >}{ 'blog ': 'http: // yw666.blog.51cto.com', 'name': 'yangwen '}
Iter of special members of the class
If an object can be iterated by a for loop, it indicates that the _ iter _ method in the object and the method contains the yield value.
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ class SpecialMembers: def _ iter _ (self ): yield 1 yield 2 yield 3 # create an object obj = SpecialMembers () # If the for loop object is executed, the _ iter _ method of the object is automatically executed, in this case, _ iter _ is a generator for I in obj: print (I)
Output
Yangwen @ Darker :~ $ Python3/home/yangwen/documentation/Python_code/sublime/Week06/Day04/s2.py123
The above is a special Python object-oriented member introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!