python--区分函数和方法, 反射

时间:2023-03-09 19:31:10
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)