ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python进阶之类的反射

2019-12-15 16:03:51  阅读:221  来源: 互联网

标签:__ 反射 进阶 python globals locals print def 函数


有应用场景的技术才是有灵魂的技术------>最近同时问我,在python中,给你一个函数或者类的字符串名称,你怎么得到该函数和类,以下结合源码记录我得到的方式:

1.给一个函数的字符串"function"得到函数并运行

class TestA(object):
    def get_test(self):
        print("我是函数1")

    def instance(self):
        print("我是函数2")


ins = TestA()
get_test = getattr(ins, "get_test")
get_test()

我们运行得到结果

C:\Users\37521\Anaconda2\python.exe C:/mine/company1/new_media_backend/newmediaBE/wechat-public/public/app/api_1_0/test.py
我是函数1

Process finished with exit code 0

通过python的反射方法getattr达到我们想要的结果,然后我们getattr函数的源码

def getattr(object, name, default=None): # known special case of getattr
    """
    getattr(object, name[, default]) -> value
    
    Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
    When a default argument is given, it is returned when the attribute doesn't
    exist; without it, an exception is raised in that case.
    """
    pass

根据源码解释getattr函数需要我们传一个对象,和一个字符串,然后从对象中找到和字符串同名的方法属性。

当然跟getattr对应的反射方法为,setattr和delattr,顾名思义这里不多解释。

2.第二个方法为eval函数

class TestA(object):
    def get_test(self):
        print("我是函数1")

    def instance(self):
        print("我是函数2")

    @staticmethod
    def test3():
        print("我是静态方法")


ins = TestA()
get_test = getattr(ins, "get_test")
get_test()
print("-----------------1-------------------")
instance = getattr(TestA, "instance")
instance(ins)
print("---------------2---------------------")
eval("instance")(ins)
print("-----------------3-------------------")
eval("TestA").test3()

我们在2处我们给eval函数传入一个instance函数字符串然后运行传出ins实例,在3出我们给eval函数传入TestA类字符串,然后运行类下的静态方法,我们看运行的结果如下

C:\Users\37521\Anaconda2\python.exe C:/mine/company1/new_media_backend/newmediaBE/wechat-public/public/app/api_1_0/test.py
我是函数1
-----------------1-------------------
我是函数2
---------------2---------------------
我是函数2
-----------------3-------------------
我是静态方法

Process finished with exit code 0

得到我们想要的结果,那我们就好奇eval函数是怎么实现可以传入一个字符串就去寻找对应的函数或者类对象,我们看一下eval函数的源码

def eval(source, globals=None, locals=None): # real signature unknown; restored from __doc__
    """
    eval(source[, globals[, locals]]) -> value
    
    Evaluate the source in the context of globals and locals.
    The source may be a string representing a Python expression
    or a code object as returned by compile().
    The globals must be a dictionary and locals can be any mapping,
    defaulting to the current globals and locals.
    If only globals is given, locals defaults to it.
    """
    pass

我们根据源码解释,首先解释器会我们传入的去locals和globals中去找我们传入的source为键找对应的值,如果未传入globals和locals就会去默认的glocals和locals中寻找,然后我们就好奇这个globals和locals是什么,然后我们运行一下

def globals(): # real signature unknown; restored from __doc__
    """
    globals() -> dictionary
    
    Return the dictionary containing the current scope's global variables.
    """
    return {}

def locals(): # real signature unknown; restored from __doc__
    """
    locals() -> dictionary
    
    Update and return a dictionary containing the current scope's local variables.
    """
    return {}

看源码解释,globals为返回一个包含全局变量的字典,locals为返回一个包含本地变量的字典,然后运行这两个函数,看一下结果:

class TestA(object):
    a=1
    def get_test(self):
        print("我是函数1")

    def instance(self):
        print("我是函数2")

    @staticmethod
    def test3():
        print("我是静态方法")

def b():
    print(222)

print(globals())
print(locals())

结果:
{'b': <function b at 0x0000000002F46978>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'C:/mine/company1/new_media_backend/newmediaBE/wechat-public/public/app/api_1_0/test.py', '__package__': None, 'TestA': <class '__main__.TestA'>, '__name__': '__main__', '__doc__': None}
{'b': <function b at 0x0000000002F46978>, '__builtins__': <module '__builtin__' (built-in)>, '__file__': 'C:/mine/company1/new_media_backend/newmediaBE/wechat-public/public/app/api_1_0/test.py', '__package__': None, 'TestA': <class '__main__.TestA'>, '__name__': '__main__', '__doc__': None}

3.根据上打印的globals函数的结果,那我们也想到根源的获取类对象的方法

globals()函数运行的结果为一个字典,类对象的字符串为建,类对象为值,所以我们可以这样得到:

class TestA(object):
    a=1
    def get_test(self):
        print("我是函数1")

    def instance(self):
        print("我是函数2")

    @staticmethod
    def test3():
        print("我是静态方法")


# print(globals())
# print(locals())
globals()["TestA"].test3()


结果如下:
C:\Users\37521\Anaconda2\python.exe C:/mine/company1/new_media_backend/newmediaBE/wechat-public/public/app/api_1_0/test.py
我是静态方法

Process finished with exit code 0
globals()["TestA"].test3()通过字典操作得到类对象,在运行类下的静态方法。

以上是我总结的3种,给出函数或类的字符串得到对应函数和类对象的方法,
从前我一直抄袭别人的博客,现在我希望能反馈输出一些有用的原创,一直在路上

标签:__,反射,进阶,python,globals,locals,print,def,函数
来源: https://www.cnblogs.com/lifei01/p/12044407.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有