Python 官方文档:入门教程 => 点击学习
反射:通过字符串映射或修改程序运行时的状态、属性、方法, 有以下4个方法 1.getattr: 2.hasattr:判断一个对象里是否有对应(相同名称)字符串的方法 3.setattr 4.delattr class Dog(object)
反射:通过字符串映射或修改程序运行时的状态、属性、方法, 有以下4个方法
1.getattr:
2.hasattr:判断一个对象里是否有对应(相同名称)字符串的方法
3.setattr
4.delattr
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self):
print ('%s is eating...'% self.name)
d = Dog('XiaoBai')
choice = input('>>:').strip() #让用户输入项调用Dog类中的功能(方法)
d.choice()
图中输入choice的内容是一个字符串,正常调用d.eat()这可不是一个字符串。
报错提示Dog中不存在attribute choice(字符串)
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self):
print ('%s is eating...'% self.name)
d = Dog('XiaoBai')
choice = input('>>:').strip()
print (hasattr(d,choice)) #判断对象d里是否有对应eat字符串的方法 #hasattr的全称就是hasattribute(有没有属性....)
执行结果:
>>:eat
True #通过这种方式就可以确认在class Dog中是否存在eat功能了
执行结果:
>>:talk
False
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self):
print ('%s is eating...' % self.name)
d = Dog('XiaoBai')
choice = input('>>:').strip()
if hasattr(d,choice): #如果d中存在eat
getattr(d,choice)() #就用getattr调用d.eat,后面不加括号的话就是打印内存信息了
#根据字符串去获取d对象里的方法的内存地址
执行结果:
>>:eat
XiaoBai is eating...
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self,food): #设置food参数
print ('%s is eating...' % self.name,food)
d = Dog('XiaoBai')
choice = input('>>:').strip()
if hasattr(d,choice):
func = getattr(d,choice) #当前func==d.eat
func('Bone') #将Bone传给food参数
setattr()
我们通过ctrl点进去setattr中
x表示对象
y被引号引着表示y是字符串
x.y=v v相当于值
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self,food):
print ('%s is eating...' % self.name,food)
def bulk(self):
print ('%s is yelling.....' %self.name)
d = Dog('XiaoBai')
choice = input('>>:').strip()
if hasattr(d,choice):
func = getattr(d,choice)
func('Bone')
else:
setattr(d,choice,bulk) #将bulk赋值给choice(这里choice=talk,也就相当于赋值给talk)
d.talk(d)
#这里choice=talk,这里d.talk=bulk,bulk本身是一个函数内存地址,将这个内存地址赋值给了talk,当前talk相当于bulk这个函数了,所以调用d.talk就相当于调用bulk这个函数,而d.bulk则是不存在的.
执行结果:
>>:talk
XiaoBai is yelling.....
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self,food):
print ('%s is eating...' % self.name,food)
def bulk(self):
print ('%s is yelling.....' %self.name)
d = Dog('XiaoBai')
choice = input('>>:').strip()
if hasattr(d,choice):
func = getattr(d,choice)
func('Bone')
else:
# setattr(d,choice,bulk)
# d.talk(d)
setattr(d,choice,22) #建立一个新的、不存在的静态属性,赋予值22
print (getattr(d,choice))
执行结果:
>>:age #建立的静态属性名称为age,然后得到赋值22
22
执行结果:
>>:name #这里输入已存在的变量名
Traceback (most recent call last):
File "E:/python/练习代码/A1.py", line 20, in <module>
func('Bone')
TypeError: 'str' object is not callable
#因为name的值已经通过func = getattr(d,choice)获取了,不能通过func('Bone')这样调用
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self,food):
print ('%s is eating...' % self.name,food)
def bulk(self):
print ('%s is yelling.....' %self.name)
d = Dog('XiaoBai')
choice = input('>>:').strip()
if hasattr(d,choice):
setattr(d,choice,'LeLe') #设置已有属性的值(相当于更改了)
else:
# setattr(d,choice,bulk)
# d.talk(d)
setattr(d,choice,22)
print (getattr(d,choice))
print (d.name)
执行结果:
>>:name
LeLe #可以看到已经不是XiaoBai了,是改过的LeLe
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self,food):
print ('%s is eating...' % self.name,food)
def bulk(self):
print ('%s is yelling.....' %self.name)
d = Dog('XiaoBai')
choice = input('>>:').strip()
if hasattr(d,choice):
delattr(d,choice) #删除属性
else:
# setattr(d,choice,bulk)
# d.talk(d)
setattr(d,choice,22)
print (getattr(d,choice))
print (d.name)
执行结果:
>>:name #指定删除name这个属性
Traceback (most recent call last):
File "E:/Python/练习代码/A1.py", line 28, in <module>
print (d.name)
AttributeError: 'Dog' object has no attribute 'name'
#可以看到name属性已不存在
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self,food):
print ('%s is eating...' % self.name,food)
def bulk(self):
print ('%s is yelling.....' %self.name)
d = Dog('XiaoBai')
choice = input('>>:').strip()
if hasattr(d,choice):
getattr(d,choice)
else:
setattr(d,choice,None)
print (d.choice) #这里是不能这么打印的,这里的choice只是单纯的一个叫choice的字符串,而不是choice = input输入的值。
AA = d.choice
print (d.name)
执行结果:
>>:age
Traceback (most recent call last):
File "E:/Python/练习代码/A2.py", line 25, in <module>
print (d.choice)
AttributeError: 'Dog' object has no attribute 'choice'
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self,food):
print ('%s is eating...' % self.name,food)
def bulk(self):
print ('%s is yelling.....' %self.name)
d = Dog('XiaoBai')
choice = input('>>:').strip()
if hasattr(d,choice):
getattr(d,choice)
else:
setattr(d,choice,None)
AA = getattr(d,choice) #需要先通过getattr获取这个choice的值
print (AA)
执行结果:
>>:age
None
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self,food):
print ('%s is eating...' % self.name,food)
def bulk(self):
print ('%s is yelling.....' %self.name)
d = Dog('XiaoBai')
choice = input('>>:').strip()
if hasattr(d,choice):
getattr(d,choice)
else:
setattr(d,choice,None)
print (d.age) #这个age就是choice=input输入的值,因为age存在所以可以直接打印
AA = getattr(d,choice) #需要先通过getattr获取这个choice的值
print (AA)
执行结果:
>>:age
None #print (d.age)
None #print (AA)
--结束END--
本文标题: Python27 反射
本文链接: https://lsjlt.com/news/190303.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0