python--区分函数和方法, 反射
1. isinstance, type, issubclass
isinstance(): 判断你给的xxx对象是否是xxxxx类型的,只支持向上判断
isinstance(object, classinfo)
type():返回xxx对象的数据类型
type(object)
type(name, bases, dict)
issubclass():判断xxx类是否是xxx的子类
class Animal:
def eat(self):
print("刚睡醒吃点儿东西") class Cat(Animal):
def play(self):
print("猫喜欢玩儿") # isinstance
c = Cat()
print(isinstance(c, Cat)) # c是一只猫
print(isinstance(c, Animal)) # 向上判断 a = Animal()
print(isinstance(a, Cat)) # 不能向下判断 # type
print(type(a)) # 返回 a的数据类型
print(type([]))
print(type(c)) # 精准的告诉你这个对象的数据类型 #issubclass
# 判断.xx类是否是xxxx类的子类
print(issubclass(Cat, Animal))
print(issubclass(Animal, Cat)) # 应用
def cul(a, b): # 此函数用来计算数字a和数字b的相加的结果
# 判断传递进来的对象必须是数字. int float
if (type(a) == int or type(a) == float) and (type(b) == int or type(b) == float):
return a + b
else:
print("对不起. 您提供的数据无法进行计算") print(cul(a, c))
2.区分方法(Method)和函数(Function)
类:
实例方法: 类型.方法 就是函数. 对象.方法 就是 方法
低级版的判定:
def func():
print("我是函数") class Foo:
def chi(self):
print("我是吃") # print(func) # <function func at 0x0000000001D42E18>
f = Foo()
# f.chi() print(f.chi) # <bound method Foo.chi of <__main__.Foo object at 0x0000000002894A90>> # 打印的结果中包含了function. 函数
# method . 方法
类方法: 都是方法
我们的类也是对象.
这个对象: 属性就是类变量
方法就是类方法
class Person:
def chi(self):
print("我要吃鱼") @classmethod
def he(cls):
print("我是类方法") @staticmethod
def pi():
print("泥溪镇地皮") p = Person()
Person.chi(1) # 不符合面向对象的思维 print(p.chi) # <bound method Person.chi of <__main__.Person object at 0x00000000028C4B70>>
print(Person.chi) # <function Person.chi at 0x00000000028989D8> 实例方法:
1. 如果使用 对象.实例方法 方法
2. 如果使用 类.实例方法 函数 print(Person.he) # <bound method Person.he of <class '__main__.Person'>>
print(p.he) # <bound method Person.he of <class '__main__.Person'>> 类方法都是 方法
print(Person.pi) # <function Person.pi at 0x0000000009E7F488>
print(p.pi) # <function Person.pi at 0x0000000009E7F488> 静态方法都是函数
高级版的判定
from types import FunctionType, MethodType # 方法和函数 class Person:
def chi(self): # 实例方法
print("我要吃鱼") @classmethod
def he(cls):
print("我是类方法") @staticmethod
def pi():
print("静态方法")
p = Person() print(isinstance(Person.chi, FunctionType)) # True
print(isinstance(p.chi, MethodType)) # True print(isinstance(p.he, MethodType)) # True
print(isinstance(Person.he, MethodType)) # True print(isinstance(p.pi, FunctionType)) # True
print(isinstance(Person.pi, FunctionType)) # True
3.反射
程序可以访问,检测和修改它本身状态或行为的一中能力(自省)好处可以实现定义好忌口,接口只有在被完成后才会真正执行,这实现了即插即用,这其实是一种后期绑定
一共用到四个函数
attr: attitude 属性的意思
getattr(obj,str) 从xxx对象中获取到xxx属性值
getattr(object, name[, default])
hasattr(obj,str) 判断xxx对象中是否有xxx属性值
hasattr(object, name)
delattr(obj,str) 从xxx对象中删除xxx属性
delattr(object, name)
setattr(obj,str,value) 设置xxx对象中的xxx属性为xxx值
setattr(object, name, value)
对于模块而言可以使用getattr, hasattr, 同样对于我们的对象也可以执行类似的操作
class Person:
def __init__(self, name, laopo):
self.name = name
self.laopo = laopo p = Person("宝宝", "林志玲") print(hasattr(p, "laopo")) #
print(getattr(p, "laopo")) # p.laopo setattr(p, "laopo", "胡一菲") # p.laopo = 胡一菲
setattr(p, "money", 100000000000) # p.money = 100000000 print(p.laopo)
print(p.money) delattr(p, "laopo") # 把对象中的xxx属性移除. != p.laopo = None
print(p.laopo)
python--区分函数和方法, 反射的更多相关文章
- python --- 19 判断对象所属,区分函数和对象, 反射
一.判断对象所属 isinstance, type , issubclass 1.issubclass(x,y) 判断x是否是y 的子类 2.type(x) 精准返回x 的数据类型 3.isi ...
- python requests函数封装方法
python requests函数封装方法 上代码 import requests import json """ 封装request请求, 1.post:my_pos ...
- python基础函数、方法
python的函数和方法,通过def 定义: 函数的特性: 减少重复代码 使程序变的可扩展 使程序变得易维护 函数和方法的区别:函数有返回值.方法没有 语法定义: def sayhi():#函数名 p ...
- Python列表函数和方法
Python列表函数和方法: 函数: len(列表名): 返回列表长度 # len(列表名): # 返回列表长度 lst = [1,2,3,'a','b','c'] print("lst 列 ...
- python之面向对象函数与方法,反射,双下方法
一.函数和方法 1.函数和方法的区别 函数: 全都是显性传参,手动传参,与对象无关 方法: 存在隐性传参,与对象有关 1.1通过函数名可以判断 len()就是函数 str.count()就是方法 de ...
- python常用函数和方法 - 备忘
语法语句篇 除法运算(精确运算和截断运算) 在python2中,除法运算通常是截断除法.什么是截断除法: >>> 3/4 0 # 自动忽略小数项 要是想 得到正确结果 怎么办呢? m ...
- python中函数与方法的区别
在python中,其实函数和方法的区别取决于其调用者,在普通的函数定义中就叫做函数 例如: def func(): print('这是一个函数') 而在一个类中定义时,就将其分为两种情况 第一种:被称 ...
- Python | Python常用函数、方法示例总结(API)
目录 前言 1. 运算相关 2. Sring与数字 3. 列表相关 4. 集合相关 5. 序列化类型 6. 字典相关 7. 输入输出 8. 文件相关 9. json模块 10. unittest测试模 ...
- Python中函数和方法的区别
方法是一种特殊的函数属于某个类的的函数叫方法不属于某个类的函数叫函数 转自csdn https://blog.csdn.net/weixin_40380298/article/details/7825 ...
随机推荐
- windows下使用nginx配置tomcat集群
转自:https://blog.csdn.net/csdn15698845876/article/details/80658599
- 实战zabbix3.0.2 使用percona mysql插件监控mysql5.7
1.系统环境 [root@shard0 templates]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.2 ...
- 迷你MVVM框架 avalonjs 学习教程2、模块化、ViewModel、作用域
一个项目是由许多人分工写的,因此必须要合理地拆散,于是有了模块化.体现在工作上,PM通常它这为某某版块,某某频道,某某页面.某一个模块,必须是包含其固有的数据,样式,HTML与处理逻辑.在jQuery ...
- Go Packages、Variables、functions
[Go Packages.Variables.functions] 1.定义包名. 2.引入Package. 3.定义导出的变量.首字母必须大写. 4.函数.Notice that the type ...
- 禁止ImageCapture自动启动
[禁止ImageCapture自动启动] 打开ImageCapture,点开左下角菜单,把Connecting this iPhone opens:的内容改为以下选项即可.
- 【Python爬虫】听说你又闹书荒了?豆瓣读书9.0分书籍陪你过五一
说明 五一将至,又到了学习的季节.目前流行的各大书单主打的都是豆瓣8.0评分书籍,却很少有人来聊聊这9.0评分的书籍长什么样子.刚好最近学了学python爬虫,那就拿豆瓣读书来练练手. 爬虫 本来思路 ...
- go_封装
go语言中首字母大写表示public go语言中首字母小写表示private 结构定义的方法必须放在同一个包内 一个目录只能放一个包 如何扩充系统的类型或别人的类型: 1.定义别名 2.使用组合 使用 ...
- Could not load TestContextBootstrapper [null]
在对SpringBoot进行单元测试时,报错“Could not load TestContextBootstrapper [null]” 错误原因: Maven的pom.xml中某些SpringBo ...
- [Linux] Big-endian and Little-endian (大小端模式)
Big-endian Little-endian 大小端模式 https://en.wikipedia.org/wiki/Endianness 大端模式,是指数据的高字节保存在内存的低地址中,而数 ...
- windows cmake安装
https://blog.csdn.net/u013832707/article/details/53127710