Python learning: object oriented -- member modifier

Member modifier

Two members

- public member

- private member, field name

- cannot directly access, only indirectly access private members through internal methods

For example: public member and private member

class Info:
    country = 'China'  # Static field    __gender = 'male'   # Static field privatization
    def __init__(self,name,age): = name
        self.__age = age  # The age field is private and cannot be accessed directly by external users
    def show(self):
        return self.__age,Info.__gender
    def __get(self):
        print('Private method')
    def gain(self):
        r = self.__get()
        return r

obj = Info('alex',19)
# print(obj.__age)  # Error will be reported at this time, unable to access
res =    # Method is in class, so private field can be accessed internally through method
# obj.__get()       # At this time, an error will also be reported and cannot be accessed
obj.gain()          # Access through internal methods
(19, 'male')
//Private method

Special members

- "init" class () auto execute

- call object () class () () automatic execution

- execute

- str str() execution

- dict object. Dict execution, which returns all contents encapsulated in the object in the form of a dictionary

Example: call method

class Info:
    def __init__(self):

    def __call__(self, *args, **kwargs):        # Object () auto execute

obj = Info()
obj()       # Execute call method only
Info()()    # Equivalent to obj()()

For example: int method str method dict method getitem method

class Info:
    def __init__(self,name,age): = name
        self.age  = age

    # Int object, automatically execute the method and assign the return value to the int object
    def __int__(self):
        return 0

    # STR object, automatically execute the str method and assign the return value to the str object
    def __str__(self):
        return '%s - %s' %(,self.age)

    def __getitem__(self, item):
        return item

obj = Info('alex',20)
print(obj)              # In fact, print() executes print(str(obj))
d = obj.__dict__
res = Info.__dict__     # View content in class
li = Info('mike',22)
res = li['APPLE']             # Automatically execute the "getitem" method in the class of li object, and "apply" is passed to the item as a parameter
alex - 20
{'name': 'alex', 'age': 20}
{'__int__': , '__getitem__': , '__str__': , '__dict__': <attribute '__dict__' of 'Info' objects>, '__init__': , '__doc__': None, '__weakref__': <attribute '__weakref__' of 'Info' objects>, '__module__': '__main__'}

metaclass, the source type of a class

Everything in python is an object. When calling a class, it passes through the type class. In python, the default metaclass = type

For example: create a Mytype class to inherit the methods in the type class. In the Mytype class, you can customize the methods you need without necessarily executing the methods in the type class

class Mytype(type):
    def __init__(self,*args,**kwargs):
        # self = Info class
        super(type, self).__init__()
    def __call__(self,*args,**kwargs):
        # self = Info class
        obj = self.__new__(self,*args,**kwargs)
        # r is the object returned in the new method of the Info class

class Info(object,metaclass=Mytype):
    def __init__(self):
        print('hello world')
# obj = Info() is actually a call to the new method in the class to create the obj object def __new__(cls,*args,**kwargs): return object.__new__(cls,*args,**kwargs) # The object is created def func(self): print('hi world') obj = Info() >>>>>>>>> hello world

Note that obj is an object and belongs to Info class

The Info class is also an object, just an object in the type class. In fact, type is also a class

In fact, the execution order of a class in the execution phase is: object = class () -- call method in the type class -- new method in the class () -- init method in the class; instead of simply calling the init method as mentioned before

Keywords: Python Attribute

Added by mohson on Sun, 01 Dec 2019 03:24:35 +0200