python反射


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

    def getName(self):
        return self.name

反射的簡單含義:

  通過類名獲得類的實例對象

  通過方法名得到方法,實現調用

反射方法一:

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

反射方法二:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM