Summary of Reflection Knowledge Points in Python
- 2021-12-13 08:35:45
- OfStack
By string mapping or modifying the state, property and method of the program at runtime, you can use the following 4 methods
'''
Use getattr(object, name_str, default=None) Method gets object Object
Memory address of the corresponding method or property
If it is an attribute: return the attribute value directly
If it is a method: Returns the memory address of the method
'''
# hasattr(object,name_str) Judge object Object has a 1 A person named name_str Method or property of
Code demo:
# -*- coding:utf8 -*-
class Person(object):
def __init__(self, name):
self.name = name
def fun(self):
print("%s Be playing " % self.name)
p1 = Person(" SB. Fly ")
name_str = input(" Please enter a method or property ").strip()
# hasattr(object,name_str) Judge object Object has a 1 A person named name_str Method or property of
if hasattr(p1, name_str):
'''
You can use it if you have it getattr(object, name_str, default=None) Method gets object Object
Memory address of the corresponding method or property
If it is an attribute: return the attribute value directly
If it is a method: Returns the memory address of the method
'''
print(getattr(p1, name_str , 80))
# >>>name: SB. Fly
# >>>fun : <bound method Person.fun of <__main__.Person object at 0x0000020B76A81370>>
# So if it is a method, you can deal with it like this
a = getattr(p1, name_str)
a()
else:
print(" The object does not have these properties and methods ")
Demonstration of judgment and acquisition
If the object does not have this method entered from the keyboard, you can use setattr to add 1 method
def bulk(self):
print(" This is in %s Method created outside the class of the object "%self.name)
class Person(object):
def __init__(self, name):
self.name = name
def fun1(self):
print("%s Be playing " % self.name)
p1 = Person(" SB. Fly ")
name_str = input(" Please enter your method or property ").strip()
if hasattr(p1, name_str):
a = getattr(p1, name_str)
a()
else: # If there is no such method, create a 1 Existing methods
"""
setattr(p1, name_str, bulk)
For the object p1 Add 1 There are already bulk The method of, named name_str
"""
setattr(p1, name_str, bulk)
a = getattr(p1, name_str)
a(p1)
"""
Running result
Please enter your method or property ui
This is a method created outside the class of someone flying objects
"""
setattr(p1, name_str, bulk) Adding method
If the object does not have this method entered from the keyboard, you can use setattr to add 1 attribute
class Person(object):
def __init__(self, name):
self.name = name
p1 = Person(" SB. Fly ")
name_str = input(" Please enter your method or property ").strip()
if hasattr(p1, name_str):
a = getattr(p1, name_str)
print(a)
# It can also be the same as setattr Fix the value of the existing property
setattr(p1, name_str, " Fly ")
print(p1.name)
else: # If this attribute does not exist, add it 1 Attributes , And set for it 1 Default values 20
setattr(p1, name_str, 20)
a = getattr(p1, name_str)
print(a)
"""
Run results:
Please enter your method or property name
SB. Fly
Fly
Run results:
Please enter your method or property age
20
"""
setattr(p1, name_str, index) Add Attributes
Delete properties and methods in objects (where methods cannot be deleted)
class Person(object):
def __init__(self, name):
self.name = name
def fun(self):
print(" This is 1 Instance method ")
p1 = Person(" SB. Fly ")
name_str = input(" Please enter your method or property ").strip()
if hasattr(p1, name_str):
# Delete the property or method of this object
delattr(p1, name_str)
else:
pass
print(p1.name)
p1.fun()
"""
Run results:
Please enter your method or property name
AttributeError: 'Person' object has no attribute 'name'
Run results:
Please enter your method or property fun
AttributeError: fun
"""
delattr(p1, name_str) You can only delete properties and dynamically added methods
Note: The method dynamically added through setattr can be deleted through delattr, which is actually an illusion. The truth is that adding a method through setattr does not really add a method to this object. Instead, one attribute was added, The second parameter of the setattr method is the name of this property, and then the value of this property is a reference address pointing to an external function, so when we call the property of this object, we actually call this function indirectly, which looks like this object has added a method 1, but it is still an added property in essence. Both setattr and delattr can only operate on the attributes of objects, and their methods on objects cannot be directly operated.