A Brief Analysis of Python reflection usage Examples
- 2020-06-19 10:51:46
- OfStack
This article illustrates the use of Python reflection. To share for your reference, specific as follows:
class Person:
def __init__(self):
self.name = "zjgtan"
def getName(self):
return self.name
Simple meaning of reflection:
Get the instance object of the class by its name
Get the method by its name and make the call
Reflection method 1:
from person import Person
theObj = globals()["Person"]()
print theObj.getName()
Reflection method 2:
module = __import__("person")
theObj = getattr(module, "Person")()
print theObj.getName()
For more information about Python, please refer to Python Data Structure and Algorithm Tutorial, Python Encryption and Decryption Algorithm and Skills Summary, Python Coding Skills Summary, Python Function Skills Summary, Python String Manipulation Skills Summary and Python Introduction and Advanced Classic Tutorial.
I hope this article has been helpful in Python programming.