1. Magic properties related to class
#Get the internal member structure of an object or class
#Get the internal doc ument of an object or class
#Get class name function name
#Class gets the class to which the current object belongs
#Get all the parent classes directly inherited by a class, return tuples
2. reflection
#Concept: use string to manipulate the attribute methods in class objects or modules
#(1) reflection in class
#hasattr() detects whether an object / class has a specified member
#getattr() gets the value of an object / class member
#setattr() sets the value of an object / class member
#delattr() deletes the value of an object / class member
#(2) module reflection
sys.modules returns a system dictionary whose key is all the loaded modules
# ### Magic properties related to classes class Human(): pass class Man(): pass class Woman(): pass class Children(Man,Woman): ''' //Function of class: describe a child //Member property of class: eye //Member method of class: sleep ﹐ beat ﹐ Doudou ''' eye = "Blue eyes" # Common method def eat(): print("Children like to eat") # Binding method def sleep(self): print("Children like to sleep,Sleep 20 hours a day") def drink(self): print("Children like to drink water,Drinking water and urine") def cry(self, func): res = func.__name__ print(res, type(res)) # Private binding method def __beat_doudou(self): print("Children like to fight beans") # __dict? Gets the internal member structure of an object or class obj = Children() res = obj.__dict__ # {} print(res) print(Children.__dict__) # {'__module__': '__main__', # '[doc]:' \ n \ Tclass function: describes the member attribute of a child's \ n \ Tclass: eye \ n \ Tclass member method: sleep \\, # 'eye': 'blue eyes', # 'eat': <function Children.eat at 0x0000000001E83D08>, # 'sleep': <function Children.sleep at 0x0000000001E83D90>, # 'drink': <function Children.drink at 0x0000000001E83E18>, # 'cry': <function Children.cry at 0x0000000001E8B1E0>, # '_Children__beat_doudou': <function Children.__beat_doudou at 0x0000000001E8B268>} # __doc > get the internal document of an object or class res = Children.__doc__ # Function of class: describe a child # Member property of class: eye # Member method of class: sleep # __beat_doudou print(res) # __Name? Get the class name and function name def myfunc(): print("I am a function.") obj.cry(myfunc) # myfunc <class 'str'> # __Class? Gets the class to which the current object belongs res = obj.__class__ # <class '__main__.Children'> print(res) # (<class '__main__.Man'>, <class '__main__.Woman'>) # __bases - gets all the parent classes directly inherited by a class, and returns tuples res = Children.__bases__ print(res)
2.Reflection part code:# Concept: use string to manipulate the attribute methods in class objects or modules# (1)hasattr() detects whether the object / class has specified members [based on whether it can be called] res = hasattr(obj, "eye") print(res) # True res = hasattr(Children, "eat") print(res) # (2)getattr() gets the value of the object / class member res = getattr(obj, "eye") print(res)
# Blue eyes res = getattr(obj,"sleep") print(res) # It returns a binding method # <bound method Children.sleep of <__main__.Children object at 0x0000000001E86DA0>> res() # Children like to sleep for 20 hours a day res = getattr(Children,"eat") print(res) # <function Children.eat at 0x00000000027C3D08> res() # Children like to eat res = getattr(Children,"sleep") # <function Children.sleep at 0x00000000027C3D90> print(res) # It's not a binding method res(123) # Children like to sleep, 20 hours a day # The third parameter of getattr is optional. If you cannot get this value, you can add a default prompt to prevent error reporting res = getattr(obj,"abc","I'm sorry, this one doesn't") print(res) # I'm sorry, this one doesn't # strvar = input("please enter the function you want to call") # if hasattr(obj,strvar): # res = getattr(obj,strvar) # res() # (3)setattr() sets the value of the object / class member setattr(obj,"hair","Shellfish yellow") print(obj.hair) # Shellfish yellow setattr(Children,"skin","green") print(Children.skin) # green # (4)delattr() deletes the value of an object / class member delattr(obj,"hair") # print(obj.hair) delattr(Children,"skin") # print(Children.skin)
(2)Reflection of modules
# sys.modules returns a system dictionary whose key is all the loaded modules
import sys
res = sys.modules
print(res)
print(__name__) #__main__
mymodule = sys.modules[__name__]
print(mymodule) # Module object
# < module '! Main ` from' H: / Python full stack video / Day25 python \x7ftcp protocol, switch, LAN, find MAC / day25 / mymodule. Py '> according to arp
def func1():
print("This is func1 Method")
def func2():
print("This is func2 Method")
def func3():
print("This is func3 Method")
# The user gives me a string and I reflect the corresponding method call
while True:
strvar = input("Please enter the method you want to call")
if hasattr(mymodule,strvar):
_func_ = getattr(mymodule,strvar)
_func_()
else:
print("Eldest brother,No,")