Article Directory
5. Combing Core Knowledge
9. Object-oriented member modifiers
9.4 Static Method
class test: def __init__(self,name): self.s_name = name @staticmethod def test_func(para): return para print (test.test_func('abc')) # Static methods are accessed through classes
Add private to test_func function
class test: def __init__(self,name): self.s_name = name @staticmethod def __test_func(para): # Add Private Underline_u return para print (test.test_func('abc'))
Calling private functions with other methods in the class
class test: def __init__(self,name): self.s_name = name @staticmethod def __test_func(para): return para def test_func01(self): return test.__test_func(self.s_name) obj =test('abc') print (obj.test_func01())
9.5 General Method
Private Functions
class test: def __init__(self,name): self.s_name = name def __test_func(self): return self.s_name obj =test('abc') print (obj.__test_func())
Calling private functions with methods in classes
class test: def __init__(self,name): self.s_name = name def __test_func(self): return self.s_name def test_func01(self): return self.__test_func() # Returns the return value of a common method decorated with a private modifier obj =test('abc') print (obj.test_func01())
Summary: Members decorated with private modifiers can only be accessed by themselves
10. Face special members such as object call, setitem, getitem, delitem
10.1 call
class test: def __init__(self,name): self.s_name = name def func(self): print (self.s_name) def __call__(self): return 'call' obj = test('abc') print (obj) # obj is an instantiated object print (obj()) # The object is directly parenthesized, and this way of executing is to execute the u call_u method in the object
10.2 setitem,getitem,delitem
- getitem
class test: def __init__(self,name): self.s_name = name def func(self): print (self.s_name) def __getitem__(self, para): return para obj = test('abc') print (obj['get_item']) # By way of the object name [para], the u getitem_u method in the object is accessed
- setitem
class test: def __init__(self,name): self.s_name = name def func(self): print (self.s_name) def __setitem__(self, key, value): print(key, value) obj = test('abc') obj['get_item']= '123' # By using the object name [key]=value, you access the u setitem_u method in the object. # Where the value set_item is passed to the key in u set item_, 123 to the value in u set item_u
- delitem
class test: def __init__(self,name): self.s_name = name def func(self): print (self.s_name) def __delitem__(self, key): print (key) obj = test('abc') del obj['get_item'] # Access the u del item_u method in the object by del object name [key], where get_item is passed to the key in u del item_u
summary
obj['get_item'] #getitem
obj['get_item'] = '123' #setitem
del obj['get_item'] #delitem
11. Special members such as Object Oriented dict
class test: def __init__(self,name): self.s_name = name def func(self): print (self.s_name) def __delitem__(self,key): print (key) obj = test('abc') print (test.__dict__) # _u dict_u in the output class test print (obj.__dict__) # _u dict_u in the output object obj
Output Results
{'__module__': '__main__', '__init__': <function test.__init__ at 0x0000022236E08708>, 'func': <function test.func at 0x0000022236E08DC8>, '__delitem__': <function test.__delitem__ at 0x0000022236E08948>, '__dict__': <attribute '__dict__' of 'test' objects>, '__weakref__': <attribute '__weakref__' of 'test' objects>, '__doc__': None} {'s_name': 'abc'}
12. Special members such as object-oriented tier
import time class test: def __init__(self): pass def __iter__(self): yield 1 time.sleep(3) yield 2 time.sleep(3) yield 3 obj = test() for i in obj: # for loop object obj print (str(i) + ' number')
The iter method in the object is automatically executed when a for loop is executed on the object, and the generator returns a value
6. Object-oriented pillars
Object-oriented has three pillars: encapsulation, inheritance, and polymorphism.
Encapsulation: Hide all hidden implementation details and expose (provide) only simple programming interfaces to the outside world
Examples of packaging:
Method defined in a class: encapsulate data and data operations