Python Reflection Notes

Source: Internet
Author: User


Reflection: Refers to the object to be executed in the form of a string or information to be obtained

The following is a demonstration of the Code execution steps:
Create a package:
Test
--__init__
--fanshe.py
--main.py

fanshe.py

GB = ' This is a global variable ' def func1 (): Return ' This is the result of the reflection function Func1 ' Def func2 (): Return ' This is the result of the reflection function Func2 ' Def func3 (): Return ' This is the reflection letter The result of a number of func3 '

main.py

#  #不算正真的反射DO  = __import__ ("Fanshe") print do. gb    #如何DO. XXXX error do.func1 ()       #执行func1函数 # The first type: Reflection Import for imported modules   fanshea = getattr (Fanshe, ' GB ')    #getattr获取fanshe模块的 ' GB ' object print ab =  getattr (Fanshe, "func1", None)     #getattr获取fanshe模块的 ' func1 ' function object, The none value is when the function specified in the string is not present in the Fanshe module, and no error print b () setattr (Fanshe, ' GB ', ' haha ') print fanshe. Gbprint hasattr (Fanshe, "GB")     #hasattr判断在模块或者类对象内是否含有某字符串对象, either returns True, NONE returns false# The second: Defines the class's reflection Class cat (object): #  class, which Cat points to this class object     def __init__ (self,  name= ' Mr.milk '):        self.name = name     def sayhi (self): #   instance method, Sayhi points to the method object, using the class or instance. Sayhi Access          print self.name,  ' says hi! '  #  access the field named name, using the instance. Name Access Cat = cat () prinT  cat.nameprint  hasattr (cat, ' name ')        # Hasattr determines whether a string object is contained within a module or class object, returns True, and none returns Falsefunc = getattr (Cat, "Sayhi", none)   # GetAttr gets the string function of the module object or Class object, and assigns the Func object func ()                                #加 () executes the Func object SetAttr (cat, "name", ' MK ')           #setattr设置 (no Add) Module object ( or class object) Print cat.namesetattr (cat, ' count ', lambda x:x+1) of the value of a function (string form) #setattr则添加count函数到cat模块对象 (or class object) r  = getattr (cat, ' count ') Print r (2)


This article is from the "Mk_linux" blog, make sure to keep this source http://wengmengkai.blog.51cto.com/9016647/1791180

Python Reflection Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.