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):
        self.name = 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.name)
# print(obj.__age)  # Error will be reported at this time, unable to access
res = obj.show()    # Method is in class, so private field can be accessed internally through method
print(res)
# obj.__get()       # At this time, an error will also be reported and cannot be accessed
obj.gain()          # Access through internal methods
>>>>>>>>>
alex
(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):
        print('init')

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

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

For example: int method str method dict method getitem method

class Info:
    def __init__(self,name,age):
        self.name = 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.name,self.age)

    def __getitem__(self, item):
        return item

obj = Info('alex',20)
print(obj)              # In fact, print() executes print(str(obj))
d = obj.__dict__
print(d)
res = Info.__dict__     # View content in class
print(res)
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
print(res)
>>>>>>>>>
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__'}
APPLE

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
        self.__init__(obj)

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