English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Simple Analysis of Python Reflection Usage Examples

This article describes the use of Python reflection. Share it with everyone for reference, as follows:

class Person:
  def __init__(self):
    self.name = "zjgtan"
  def getName(self):
    return self.name

The simple meaning of reflection:

Obtain the instance object of the class by class name

Obtain the method by method name to achieve the call

Reflection Method One:

from person import Person
theObj = globals()["Person"]()
print theObj.getName()

Reflection Method Two:

module = __import__("person")
theObj = getattr(module, "Person")()
print theObj.getName()

Readers who are interested in more about Python-related content can check the special topics of this site: 'Python Data Structures and Algorithms Tutorial', 'Python Encryption and Decryption Algorithms and Techniques Summary', 'Python Coding Operation Skills Summary', 'Python Function Usage Skills Summary', 'Python String Operation Skills Summary', and 'Python入门与进阶经典教程'.

I hope the content described in this article will be helpful to everyone in Python program design.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report by email to codebox.com (replace # with @ when sending email) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like