设为首页 加入收藏

TOP

day7面向对象--反射(一)
2017-09-30 17:28:47 】 浏览:1311
Tags:day7 面向 对象 反射

反射

    通过字符串映射或修改程序运行时的状态、属性、方法, 有以下4个方法

    1、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.

    判断类中是否有指定的方法,如下:

class Dog(object):

    def __init__(self,name):
        self.name = name

    def eat(self):
        print("%s is eating......" %self.name)

d = Dog("alex")
choice = input(">>:").strip()    #用户输入函数,判断函数是否存在,存在执行,反射来判断执行
print(hasattr(d,choice))
运行如下:
>>:talk
False
>>:eat
True

    我们知道,有时候我们要根据用户输入来判断类中是否有某种方法,这样我们就能执行了。如何去判断呢?可以使用hasattr(object,name)方法。上面,我们实现了动态判断方法,如果存在返回True,否则返回False。

    2、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.

class Dog(object):

    def __init__(self,name):
        self.name = name

    def eat(self):
        print("%s is eating......" %self.name)

d = Dog("alex")
choice = input(">>:").strip()    #用户输入函数,判断函数是否存在,存在执行,反射来判断执行
print(getattr(d,choice))
运行结果如下:
>>:tall
Traceback (most recent call last):
  File "/home/zhuzhu/第七天/get_attr.py", line 11, in <module>
    print(getattr(d,choice))
AttributeError: 'Dog' object has no attribute 'tall'
>>:eat
<bound method Dog.eat of <__main__.Dog object at 0x7fecf92428d0>>

    从上面可以看出,getattr()是获取属性的内存地址,如果存在返回内存地址,如果不存在,则返回错误,提示类中不存在这个方法。

    既然getattr()存在放回方法的内存地址,那么加上括号执行一下,如下:

class Dog(object):

    def __init__(self,name):
        self.name = name

    def eat(self):
        print("%s is eating......" %self.name)

d = Dog("alex")
choice = input(">>:").strip()    #用户输入函数,判断函数是否存在,存在执行,反射来判断执行
getattr(d,choice)()
运行结果如下:
>>:eat
alex is eating......

    从上面可以看出,程序正常执行了,通过上面,我们可以发现,hasattr()和getattr()经常结合一起使用,hasattr()判断是否有这个方法,而getattr()用来执行。如果存在,则调用getattr()进行执行。如下:

class Dog(object):

    def __init__(self,name):
        self.name = name

    def eat(self):
        print("%s is eating......" %self.name)

d = Dog("alex")
choice = input(">>:").strip()    #用户输入函数,判断函数是否存在,存在执行,反射来判断执行
if hasattr(d,choice):
    '''这种场景在很多情况下都使用,比如让用户输入某个功能,如果存在执行,不存在则告诉用户没有这个功能'''
    getattr(d,choice)()

else:
    print("您输入的方法不存在,请核对后重新输入:")
运行结果如下:
>>:eat #存在执行方法
alex is eating......
>>:tall #不存在,提示方法没有
您输入的方法不存在,请核对后重新输入

    从上面代码可以看出,能够实现动态用户输入判断方法是否存在,存在执行,不存在提示的功能。

    下面是实现带参数的情况:

class Dog(object):

    def __init__(self,name):
        self.name = name

    def eat(self,food):
        print("%s is eating......%s" %(self.name,food))

d = Dog("alex")
choice = input(">>:").strip()    #用户输入函数,判断函数是否存在,存在执行,反射来判断执行
if hasattr(d,choice):
    '''这种场景在很多情况下都使用,比如让用户输入某个功能,如果存在执行,不存在则告诉用户没
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇day6面向对象--类的特殊成员方法 下一篇学习笔记TF034:实现Word2Vec

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目