向日葵223

导航

 

概述

反射实现动态装配,通过字符串来反射类中的属性和方法

反射函数

1、hasattr(obj,name_str)           作用:判断一个对象obj是否存在name_str的属性或者方法

 

 1 class Dog(object):
 2     def __init__(self, name):
 3         self.name = name
 4 
 5     def eat(self):
 6         print("%s eating...."% self.name)
 7 d = Dog("JJ")
 8 choice = input(">>>").strip()
 9 print(hasattr(d, choice))
10 
11 结果:
12 >>>ss
13 False
14 
15 或者
16 
17 >>>eat
18 True

 

2、getattr(obj, name_str)        作用:根据字符串name_str获取obj对象中的内存地址或者对应的属性值

 

class Dog(object):
    def __init__(self, name):
        self.name = name

    def eat(self):
        print("%s eating...."% self.name)
d = Dog("JJ")
choice = input(">>>").strip()
if hasattr(d, choice):
    print(getattr(d,choice))   # 通过choice输入的字符串从d对象里寻找


结果:
>>>eat
<bound method Dog.eat of <__main__.Dog object at 0x0053C2F0>>

 3、setattr(x,y,z)       作用:给obj添加一个新的属性或者方法

1)给对象新增一个方法

 1 def bulk(self):
 2     print("%s is yelling"% self.name)
 3 
 4 
 5 class Dog(object):
 6     def __init__(self, name):
 7         self.name = name
 8 
 9     def eat(self):
10         print("%s eating...."% self.name)
11 d = Dog("JJ")
12 choice = input(">>>").strip()
13 setattr(d, choice, bulk)
14 # d.talk(d)  # 直接写死了,一般不用这种
15 func = getattr(d, choice) # 用getattr来获取
16 func(d)
17 
18 结果:
19 >>>talk
20 JJ is yelling

 

2)给对象新增一个属性

 1 class Dog(object):
 2     def __init__(self, name):
 3         self.name = name
 4 
 5     def eat(self):
 6         print("%s eating...."% self.name)
 7 d = Dog("JJ")
 8 choice = input(">>>").strip()
 9 setattr(d, choice, 22)
10 # print(d.age)  #直接写死了  一般不这么写
11 print(getattr(d, choice))
12 
13 结果:
14 >>>number
15 22

4、delattr(x, y)     作用:删除obj对象中的属性和方法,delattr(x, 'y') is equivalent to ``del x.y''

 1 class Dog(object):
 2     def __init__(self, name):
 3         self.name = name
 4 
 5     def eat(self):
 6         print("%s eating...."% self.name)
 7 d = Dog("JJ")
 8 choice = input(">>>").strip()
 9 delattr(d, choice)
10 print(d.eat)
11 print(d.name)
12 
13 结果:
14 >>>eat
15 Traceback (most recent call last):
16   File "E:/python_3.5/stu/反射模块.py", line 14, in <module>
17     delattr(d, choice)
18 AttributeError: eat

 

1、hasattr

2、getattr

3、setattr

4、delattr

1)添加一个新方法

 1 def bulk(self):
 2     print("yelling......")
 3 
 4 class Dog(object):
 5     def __init__(self, name, age):
 6         self.name = name
 7         self.age = age
 8 
 9     def eat(self, food):
10         print("%s eating....%s"%(self.name, food))
11 
12 d = Dog("Lidy", 2)
13 choice = input(">>>")
14 if hasattr(d, choice):
15     fun = getattr(d, choice)
16     fun("baozi")
17 else:
18     setattr(d, choice, bulk)
19     # print(getattr(d, choice))
20     fun = getattr(d, choice)
21     fun(d)
22 
23 
24 结果:
25 >>>talk
26 yelling......
27 
28 或者
29 >>>eat
30 Lidy eating....baozi

2)添加新的属性

 1 def bulk(self):
 2     print("yelling......")
 3 
 4 class Dog(object):
 5     def __init__(self, name, age):
 6         self.name = name
 7         self.age = age
 8 
 9     def eat(self, food):
10         print("%s eating....%s"%(self.name, food))
11 
12 d = Dog("Lidy", 2)
13 choice = input(">>>")
14 if hasattr(d, choice):
15     attr = getattr(d, choice) 
16 else:
17     setattr(d, choice, 222)
18 
19 print(getattr(d, choice))
20 
21 结果:
22 >>>name
23 Lidy
24 
25 或者
26 >>>eee
27 222

3)删除属性

 1 def bulk(self):
 2     print("yelling......")
 3 
 4 class Dog(object):
 5     def __init__(self, name, age):
 6         self.name = name
 7         self.age = age
 8 
 9     def eat(self, food):
10         print("%s eating....%s"%(self.name, food))
11 
12 d = Dog("Lidy", 2)
13 choice = input(">>>")
14 if hasattr(d, choice):
15     delattr(d, choice)
16     # attr = getattr(d, choice)
17 else:
18     setattr(d, choice, 222)
19 
20 print(getattr(d, choice))
21     # fun = getattr(d, choice)
22     # fun(d)
23 
24 结果:
25 >>>name
26 Traceback (most recent call last):
27   File "E:/python_3.5/stu/类/反射.py", line 22, in <module>
28     print(getattr(d, choice))
29 AttributeError: 'Dog' object has no attribute 'name'

 

posted on 2017-12-23 11:11  向日葵223  阅读(77)  评论(0编辑  收藏  举报