iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python27 反射
  • 410
分享到

Python27 反射

反射 2023-01-31 06:01:13 410人浏览 泡泡鱼

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()

image_1c17eg6q8fcj1td2bnq1jo01vnb9.png-13.4kB
图中输入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中

image_1c17gckbcjhda4k1v8p1q2km16m.png-16.9kB
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://www.lsjlt.com/news/190303.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python27 反射
    反射:通过字符串映射或修改程序运行时的状态、属性、方法, 有以下4个方法 1.getattr: 2.hasattr:判断一个对象里是否有对应(相同名称)字符串的方法 3.setattr 4.delattr class Dog(object)...
    99+
    2023-01-31
    反射
  • 【javaSE】 反射与反射的使用
    文章目录 🌲反射的定义🎍反射的用途🌴反射基本信息🍀反射相关的类🚩Class类(反射机制的起源 )🎈...
    99+
    2023-09-16
    开发语言 java 反射
  • Unity3d 射线反射
    As any person that has already used Unity’s Ray class knows, there’s no support for reflection, which could be useful fo...
    99+
    2023-01-31
    射线 反射 Unity3d
  • PHP 反射
    阅读目录 参考文档Laravel 通过类的反射, 对类的私有属性赋值TestController 类TwoTestController 类预览 PHP中的反射理解PHP反射API均以 Re...
    99+
    2023-09-02
    php 开发语言 前端
  • python 反射
    1.反射 主要是用到了4个函数(  用的最多的就是getattr()和 hasattr()  ): getattr()   从xxx对象中获取到xxx属性值 hasattr()  判断xxx对象中是否有xxx属性值delattr()   ...
    99+
    2023-01-30
    反射 python
  • Python27+Opencv3 Vid
    最近在win10 X64部署了Python27 win32 + Opencv3的环境,具体过程记录于其他博文。进行图片的相关操作等均正常,但是在操作视频时,出现了问题。具体如下代码:Cap = cv2.VideoCapture('*****...
    99+
    2023-01-31
    Vid
  • Python反射
    反射的定义根据字符串的形式去某个对象中操作成员根据字符串的形式去一个对象中寻找成员根据字符串的形式去一个对象中设置成员根据字符串的形式去一个对象中删除成员根据字符串的形式去一个对象中判断成员是否存在初始反射通过字符串的形式,导入模块根据用户...
    99+
    2023-01-31
    反射 Python
  • Python-----反射
    class Person(object): """定义一个人类""" def __init__(self, name): self.name = name def eat(self, food): ...
    99+
    2023-01-31
    反射 Python
  • AS3反射
    反射这玩意,在一些游戏的框架中的确有其优势,但是注意反射或多或少会影响性能的.在资源的获取上就 使用了反射 , 得到SWF中的美术的资源 如:public static function getClazz(className : Strin...
    99+
    2023-01-31
    反射
  • Python_反射
    一.反射定义放射是指程序可以访问。检测和修改它本身状态或行为的一种能力(自省)。二.四个自省的函数Python中提供了以下四种自省的函数,使用于类和对象。1.hasattr函数--用于判断obj中有没有name字符串对应的方法或属性,若有返...
    99+
    2023-01-31
    反射
  • Python 反射-isinstance
    用到的 isinstance(对象,类)  -------------------  判断一个对象是否是一个类的实例 issubclass(子类,父类)  ----------------  判断一个类是否是一个类的子类 hasattr(...
    99+
    2023-01-30
    反射 Python isinstance
  • python之反射
    1. isinstance, type, issubclass的区别 class Animal: def eat(self): print("刚睡醒吃点儿东西") class Cat(Animal): ...
    99+
    2023-01-30
    反射 python
  • JavaSE基础之反射机制(反射Class)详解
    目录一:反射机制概述二:反射Class1. 获取Class的三种方式 2. 通过反射实例化(创建)对象3. 通过读配置属性文件实例化对象4. 只让静态代码块执行5. 获取类...
    99+
    2024-04-02
  • AJPFX反射及反射的应用该如何理解
    本篇文章为大家展示了AJPFX反射及反射的应用该如何理解,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。怎么理解反射,反射的应用        反射就是把Jav...
    99+
    2023-06-02
  • golang 去除反射
    近年来,Go语言(Golang)以其简洁、高效和高可靠性备受开发者的青睐。其中,反射机制是Golang的一大特色,它使得程序能够在运行时动态地获取变量的类型和值,使得开发者可以对程序进行更加灵活的控制,在很多场景下大显身手。然而,反射机制也...
    99+
    2023-05-19
  • golang 反射 方法
    一、反射的概念在程序设计中,反射是指在运行时获取程序结构信息的一种能力。简单解释,就是程序可以在运行时自己检查自己的状态、属性和方法,而不用在编译的时候确定所有的这些信息。反射能够让Go语言程序也拥有类似于其他动态语言的灵活性和能力。在Go...
    99+
    2023-05-22
  • Python基础:反射
    反射就是根据提供的字符串,匹配对象(类、模块等)里面的方法。达到动态调用的目的。主要有四个成员。getattr、hasattr、setattr、delattr  获取成员、检查成员、设置成员、删除成员语法:getattr(对象,字符串)se...
    99+
    2023-01-31
    反射 基础 Python
  • php有反射吗
    这篇文章主要讲解了“php有反射吗”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“php有反射吗”吧!php有反射,php具有完整的反射API,增加了内省类、接口、函数、方法和扩展的能力; 此...
    99+
    2023-06-22
  • Python27+Opencv3 捕获网
    Opencv3+Python比较常见的是播放本地avi视频文件、或者捕获PC自带摄像头视频。现在网络摄像机遍布,而我们测试时也需要用到网络摄像机的实时视频,并进行处理,参考《opencv3计算机视觉(python语言实现)》编写了捕获网络摄...
    99+
    2023-01-31
  • Java 反射机制
    简介: Java的反射(reflection)机制是指在程序的运行状态中,可以构造任意一个类的对象,可以了解任意一个对象所属的类,可以了解任意一个类的成员变量和方法,可以调用任意一个...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作