python: advanced object-oriented

Keywords: Python

1. Reflection

Reflection: using string type names to manipulate variables
Reflection has no security problem. It can operate the existing variables in memory
 
#Attributes and methods in reflective objects
class A:
    price=20
print(getattr(A,'price'))

#Reflecting object properties

class A:
    def func(self):
        print('in func')
a =A()
a.name ='alex'

ret =getattr(a,'name')#The value obtained through the string form of variable name
print(ret)
#Methods of reflecting objects
ret =getattr(a,'func')
ret()

#Methods of reflection class:

if hasattr(A,'func')):
    getattr(A,'func')()

 

#Properties of reflection class

class A: 
    price=20
print(getattr(A,'price'))

#Properties of reflection module

import my
print(getattr(my,'day'))
#Reflect variables in your own module
import sys
print(getattr(sys.modules['__main__'],'year'))
getattr(sys.modules['__main__'],'qqxing')()

setattr set / modify variables

class A:

  pass

setattr(A,'name','alex')

print(A,name)

delattr delete variable

delattr(a,'name')

2. STR and repr__

Change the string display of the object__

Custom format string__

# Double down method
# obj.__str__  str(obj)
# obj.__repr__ repr(obj)
class Teacher:
    def __init__(self,name,salary):
        self.name =name
        self.salary =salary
    def __str__(self):
        return "Teacher's object :%s"%self.name
    def __repr__(self):
        return str(self.__dict__)
    def func(self):
        return 'wahaha'
nezha =Teacher('nazha',250)
print(nazha)
print(repr(nezha))
print('>>> %r'%nezha)
#object  One of them__str__,Once called, returns the#Object's memory address
l = [1,2,3,4,5]   # Instantiate an object that instantiates a list class
print(l)
# %s str()  Direct printing actually goes__str__
# %r repr()  Actually, it's all gone__repr__
# repr yes str But str Can't do it repr The spare wheel of
# print(obj)/'%s'%obj/str(obj)It's actually an internal call obj.__str__Method, if str Method, it must return a string
# without__str__Method, first find the__repr__Method, no more__str__. 
# repr(),I'll just find__repr__,If no parent is found
class Classes:
    def __init__(self,name):
        self.name = name
        self.student = []
    def __len__(self):
        return len(self.student)
    def __str__(self):
        return 'classes'
py_s9= Classes('python Phase 9 of the whole stack')
py_s9.student.append('Second elder brother')
py_s9.student.append('Thai brother')
print(len(py_s9))
print(py_s9)
#__del__
class A:
    def __del__(self):   # Destructor: Do some finishing before deleting an object
        self.f.close()
a = A()
a.f = open()   # Open the file first, open a file in the operating system, get the file operator and store it in memory
del a          # a.f Got the file operator and disappeared in memory
del a   # del Both this method and the variables are deleted
# __call__
class A:
    def __init__(self,name):
        self.name = name
    def __call__(self):
        '''
        //Print all properties in this object
        :return:
        '''
        for k in self.__dict__:
            print(k,self.__dict__[k])
a = A('alex')()

 

 

Posted by ts2000abc on Thu, 30 Apr 2020 02:00:32 -0700