Python object oriented special member
- 2020-05-30 20:26:40
- OfStack
Class special member of call
#!/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 constructor
def __call__(self):
print("My Name is: Yangwen")
# create 1 And execute the constructor of the class
obj = SpecialMembers()
# Executes the constructor of the object
obj()
# First execute the class constructor, then the object constructor
SpecialMembers()()
The output
yangwen@Darker:~$ python3 /home/yangwen/ The document /Python_code/sublime/Week06/Day04/s2.py
My Blog is Url: http://yw666.blog.51cto.com
My Name is: Yangwen
My Blog is Url: http://yw666.blog.51cto.com
My Name is: Yangwen
Class getitem, setitem, delitem
#!/usr/bin/env python
# _*_coding:utf-8 _*_
class SpecialMembers:
# When you perform obj['value'] Will be automatically executed __getitem__ Method and treat the value inside the object's parentheses as __getitem__ The value of the
def __getitem__(self, item):
print(item)
def __setitem__(self, key, value):
print(key, value)
def __delitem__(self, key):
print(key)
# create 1 An object
obj = SpecialMembers()
# automated __getitem__ methods
obj['value']
# automated __setitem__ methods
obj['k1'] = "values"
# automated __delitem__ methods
del obj['key']
The output
ansheng@Darker:~$ python3 /home/yangwen/ The document /Python_code/sublime/Week06/Day04/s2.py
value
k1 values
key
special
#!/usr/bin/env python
# _*_coding:utf-8 _*_
class SpecialMembers:
# When you perform obj['value'] Will be automatically executed __getitem__ Method and treat the value inside the object's parentheses as __getitem__ The value of the
def __getitem__(self, item):
print(item, type(item), "__getitem__")
def __setitem__(self, key, value):
print(key, value)
def __delitem__(self, key):
print(key)
# create 1 An object
obj = SpecialMembers()
# automated __getitem__ methods
obj[1:3] # __getslice__/__getitem__
# automated __setitem__ methods
obj[1:3] = [11, 22, 33] # __setslice__/__setitem__
# automated __delitem__
del obj[1:3] # __delslice__/__delitem__
The output
yangwen@Darker:~$ python3 /home/yangwen/ The document /Python_code/sublime/Week06/Day04/s2.py
slice(1, 3, None) <class 'slice'> __getitem__
slice(1, 3, None) [11, 22, 33]
slice(1, 3, None)
dict for a special member of class
Gets all members of a class or object
#!/usr/bin/env python
# _*_coding:utf-8 _*_
class SpecialMembers:
"""
Class notes
"""
def __init__(self):
self.Name = "Ansheng"
self.Blog = "http://yw666.blog.51cto.com"
# Gets a member of a class
print(SpecialMembers.__dict__)
# create 1 An object
obj = SpecialMembers()
# Gets a member of an object
print(obj.__dict__)
The output
yangwen@Darker:~$ python3 /home/yangwen/ The document /Python_code/sublime/Week06/Day04/s2.py
{'__weakref__': <attribute '__weakref__' of 'SpecialMembers' objects>, '__doc__': '\n Class notes \n ', '__module__': '__main__', '__dict__': <attribute '__dict__' of 'SpecialMembers' objects>, '__init__': <function SpecialMembers.__init__ at 0x7ff2af2d7598>}
{'Blog': 'http://yw666.blog.51cto.com', 'Name': 'Yangwen'}
Class special member of iter
If one object can be iterated by the for loop, then the method with yield value in the object is indicated.
#!/usr/bin/env python
# _*_coding:utf-8 _*_
class SpecialMembers:
def __iter__(self):
yield 1
yield 2
yield 3
# create 1 An object
obj = SpecialMembers()
# If you execute for When the object is looped, the object is automatically executed __iter__ Method, at this point __iter__ is 1 A generator
for i in obj:
print(i)
The output
yangwen@Darker:~$ python3 /home/yangwen/ The document /Python_code/sublime/Week06/Day04/s2.py
1
2
3