python反射

摘要:
Isinstance:判断对象是否属于该类(up)类型:返回对象的数据类型issubclass:判断类是否是该类的子类1classAnimal:2defchi(self):3print('eat')4classCat(Animal):5defhe(sell):6print('drink')7c=Cat()8print(Isinstance(c,Cat))#判断c是否是Cat对象9pri

isinstance: 判断对象是否是属于这个类(向上判断)
type: 返回某对象的数据类型
issubclass: 判断某类是否是这个类的子类

复制代码
 1 class Animal:
 2     def chi(self):
 3         print('吃饭')
 4 class Cat(Animal):
 5     def he(self):
 6         print('喝水')
 7 c = Cat()
 8 print(isinstance(c,Cat))   #判断c是否是Cat的对象
 9 print(isinstance(c,Animal)) #判断c是否是Animal的对象,只能向上判断
10 a = Animal()
11 print(isinstance(a,Cat))   #不能向下判断
12 
13 精准的判断这个对象属于哪个类
14 print(type(a))
15 print(type(c))
16 
17 判断某类是否是这个类的子类
18 print(issubclass(Cat,Animal))
19 print(issubclass(Animal,Cat))
20 结果
21 True
22 True
23 False
24 <class '__main__.Animal'>
25 <class '__main__.Cat'>
26 True
27 False
复制代码

事例

复制代码
1 def cul(a,b):
2     if (type(a) == int or type(a) == float) and (type(b) == int or type(b) == float):
3         return a + b
4     else:
5         return '无法计算'
6 print(cul('sad',13))
7 结果
8 无法计算
复制代码

区分方法和函数(代码)
野路子: 打印的结果中包含了function的是函数,包含method的是方法

复制代码
 1 def func():
 2     print('我是函数')
 3 class Foo:
 4     def chi(self):
 5         print('吃饭')
 6 print(func)
 7 f = Foo()
 8 f.chi()
 9 print(f.chi)
10 结果
11 <function func at 0x0000025A57361E18>
12 吃饭
13 <bound method Foo.chi of <__main__.Foo object at 0x0000025A590573C8>>
复制代码

在类中:(类也是对象)
实例方法
    如果是类名.方法  函数
    如果是对象.方法  方法
类方法: 都是方法
静态方法: 都是函数

复制代码
 1 class Person:
 2     def chi(self):
 3         print('我是实例方法')
 4     @classmethod
 5     def he(cls):
 6         print('我是类方法')
 7     @staticmethod
 8     def wa():
 9         print('我是静态方法')
10 p = Person()
11 print(p.chi)
12 Person.chi(1)       # 不符合面向对象的思维(不建议用)
13 print(Person.chi)
14 
15 类方法: 都是方法
16 print(Person.he)
17 print(p.he)
18 
19 静态方法: 都是函数
20 print(Person.wa)
21 print(p.wa)
22 结果
23 <bound method Person.chi of <__main__.Person object at 0x000002437C2B73C8>>
24 我是实例方法
25 <function Person.chi at 0x000002437C2B98C8>
26 <bound method Person.he of <class '__main__.Person'>>
27 <bound method Person.he of <class '__main__.Person'>>
28 <function Person.wa at 0x000002437C2B99D8>
29 <function Person.wa at 0x000002437C2B99D8>
复制代码

判断是否是方法或函数

复制代码
 1 from types import MethodType, FunctionType
 2 isinstance()
 3 
 4 from types import FunctionType,MethodType
 5 class Person:
 6     def chi(self):
 7         print('我是实例方法')
 8     @classmethod
 9     def he(cls):
10         print('我是类方法')
11     @staticmethod
12     def wa():
13         print('我是静态方法')
14 p = Person()
15 print(isinstance(Person.chi,FunctionType))
16 print(isinstance(p.chi,MethodType))
17 
18 print(isinstance(p.he,MethodType))
19 print(isinstance(Person.he,MethodType))
20 
21 print(isinstance(p.wa,FunctionType))
22 print(isinstance(Person.wa,FunctionType))
23 结果
24 True
25 True
26 True
27 True
28 True
29 True
复制代码

反射
对于模块而言可以使用getattr,hasattr,同样对于对象也可以执行类似的操作
getattr(): 从某对象中获取到某属性值
hasattr(): 判断某对象中是否有某属性值

复制代码
 1 在一个文件中写一个py文件
 2 def chi():
 3     print('吃饭')
 4 def he():
 5     print('喝水')
 6 
 7 再在另外一个文件中判断
 8 import test2
 9 while 1:
10     content = input("<<:")
11     if hasattr(test2,content):
12         print('有这个功能')
13         ret = getattr(test2,content)
14         ret()
15     else:
16         print('没有这个功能')
17 结果
18 <<:chi
19 有这个功能
20 吃饭
21 <<:hh
22 没有这个功能
复制代码

delattr(): 从某对象中删除某个属性
setattr(): 设置某对象中的某个属性为xxx

复制代码
 1 class Person:
 2     def __init__(self,name,addr):
 3         self.name = name
 4         self.addr = addr
 5 p = Person('jack','北京')
 6 print(hasattr(p,'addr'))    #判断这个属性是否存在
 7 print(getattr(p,'addr'))    #打印属性值
 8 setattr(p,'addr','上海')    #修改属性值p.addr='上海'
 9 setattr(p,'money',9999999)  #相当于新添加一个属性money=999999
10 print(p.addr)
11 print(p.money)
12 delattr(p,'addr')
13 # print(p.addr)     #因为addr这个属性被删除了,所以打印报错
14 结果
15 True
16 北京
17 上海
18 9999999

免责声明:文章转载自《python反射》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vue初级学习--组件的使用(自定义组件)任务栏-添加我的电脑下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

eclipse安装插件的方法,以python为例子

一 转载自:http://www.cnblogs.com/linzhenjie/articles/2639113.html 1、基本需求     1、Eclipse 集成开发环境下载 http://115.com/file/c2vz7io5    JDK6下载 http://115.com/file/c2vz7idq     2、Pydev插件下载  ht...

《Spark Python API 官方文档中文版》 之 pyspark.sql (三)

摘要:在Spark开发中,由于需要用Python实现,发现API与Scala的略有不同,而Python API的中文资料相对很少。每次去查英文版API的说明相对比较慢,还是中文版比较容易get到所需,所以利用闲暇之余将官方文档翻译为中文版,并亲测Demo的代码。在此记录一下,希望对那些对Spark感兴趣和从事大数据开发的人员提供有价值的中文资料,对PyS...

作业一 统计软件简介与数据操作

       SPSS(Statistical Product and Service Solutions),"统计产品与服务解决方案"软件。最初软件全称为"社会科学统计软件包"(SolutionsStatistical Package for the Social Sciences),但是随着SPSS产品服务领域的扩大和服务深度的增加,SPSS公司已于2...

Web自动化测试之playwright:概述

playwright是由微软开发的Web UI自动化测试工具, 支持Node.js、Python、C# 和 Java语言,本文将介绍playwright的特性以及它的简单使用。 目录 playwright特性 安装 命令行工具 脚本录制 打开网页 截图 同步和异步API 浏览器 浏览器上下文 多页面 断言 playwright特性 play...

4.1. 如何在Windows环境下开发Python

4.1. 如何在Windows环境下开发Python 4.1. 如何在Windows环境下开发Python 4.1.1. Python的最原始的开发方式是什么样的 4.1.1.1. 找个文本编辑器,新建个.py文件,写上Python代码 4.1.1.2. 打开Windows的cmd,并且切换到对应的python脚本所在目录 4.1.1.2.1....

python 安装PyV8 和 lxml

近来在玩python爬虫,需要使用PyV8模块和lxml模块。但是执行pip install xx 或者easy_install xx 指令都会提示一些错误。这些错误有些是提示pip版本过低或者缺少vc++ 9.0环境,再或者一些头文件无法引用等等。我也懒得找错误解决方法。就直接下载Pyv8模块的安装包和lxml的安装包。 Pyv8的安装包链接: 1.针对...