####################总结##########

1.

isinstance: 判断xxx是否是xxx类型的(向上判断)
type: 返回xx对象的数据类型
issubclass: 判断xxx类是否是xxx类的子类

type: 返回xx对象的数据类型

def add(a, b):
if (type(a) == int or type(a) == float) and (type(b) == int or type(b) == float):
return a + b
else:
print("算不了") print(add("胡汉三", 2.5))

issubclass: 判断xxx类是否是xxx类的子类

class Animal:
pass
class Cat(Animal):
pass
class BoSiCat(Cat):
pass
print(issubclass(Cat, Animal)) # 判断第一个参数是否是第二个参数的后代
print(issubclass(Animal, Cat))
print(issubclass(BoSiCat, Animal)) # True #结果
True
False
True

2.

区分函数和方法

#方法一

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 . 方法
#方法二
from collections import Iterable, Iterator # 迭代器
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

3.反射

1. hasattr(对象, str) 判断对象中是否包含str属性
2. getattr(对象, str) 从对象中获取到str属性的值
3. setattr(对象, str, value) 把对象中的str属性设置为value
4. delattr(对象, str) 把str属性从对象中删除

class Person:
def __init__(self,name,laopo):
self.name=name
self.laopo=laopo
p= Person('宝宝','林志玲') print(hasattr(p,'laopo'))
print(getattr(p,'laopo'))
setattr(p,'laopo','胡一菲')
setattr(p,'money',1000) print(p.laopo)
print(p.money)
delattr(p, "laopo") # 把对象中的xxx属性移除. != p.laopo = None
print(p.laopo) ########结果
True
林志玲
胡一菲
1000 Traceback (most recent call last):
File "D:/python_work_s18/day18关系/test.py", line 87, in <module>
print(p.laopo)
AttributeError: 'Person' object has no attribute 'laopo' Process finished with exit code 1

实例二

####单独文件master
def chi():
print("大牛很能吃") def he():
print("大牛一次喝一桶") def shui():
print("大牛一次睡一年") def play_1():
print("大牛不玩儿压缩了. 玩儿儿童节") def sa():
print("大牛很能撒谎") def la():
print("大牛喜欢拉二胡")
while 1:
content = input("请输入你要测试的功能:") # 用户输入的功能是一个字符串 # 判断 xxx对象中是否包含了xxxxx
if hasattr(master, content):
xx = getattr(master, content)
xx()
print("有这个功能")
else:
print('没有这个功能')

python 面向对象(四)反射的更多相关文章

  1. Python 面向对象(四) 反射及其魔术方法

    反射 reflection 也有人称之为自省 作用: 运行时获取.添加对象的类型定义信息,包括类 内建方法: getattr(object, name[, default])   返回object对象 ...

  2. Python 面向对象之反射

    Python 面向对象之反射 TOC 什么是反射? hasattr getattr setattr delattr 哪些对象可以使用反射 反射的好处 例子一 例子二 什么是反射? 程序可以访问.检查和 ...

  3. python 面向对象之反射及内置方法

    面向对象之反射及内置方法 一.静态方法(staticmethod)和类方法(classmethod) 类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属性交互(也就是可以使用类属性) 静 ...

  4. Python面向对象之反射,双下方法

    一. 反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被程序 ...

  5. python面向对象进阶 反射 单例模式 以及python实现类似java接口功能

    本篇将详细介绍Python 类的成员.成员修饰符.类的特殊成员. 类的成员 类的成员可以分为三大类:字段.方法和特性. 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存 ...

  6. Python面向对象之-反射

    Python中一切皆对象,在Python中的反射:通过字符串的形式操作对象的属性 hasattr  判断是否有改属性或者方法,有返回True,没有返回false getattr  如果是属性获得该属性 ...

  7. python面向对象的反射

    python面向对象中的反射:通过字符串的形式操作对象相关的属性.python中的一切事物都是对象(都可以使用反射) getattr # 根据字符串的形式,去对象中找成员.hasattr # 根据字符 ...

  8. 第三十四篇 Python面向对象之 反射(自省)

    什么是反射? 反射的概念是由Smith在1982年提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被程序语 ...

  9. Python面向对象之反射

    一.反射的基本概念 二.反射示例 三.反射的应用 一.反射的基本概念 反射:可以用字符串的方式去访问对象的属性,调用对象的方法(但是不能去访问方法),Python中一切皆对象,都可以使用反射. 反射有 ...

随机推荐

  1. vscode——配置git的path

    设置 打开设置 搜索配置 复制Json文本 编辑配置 粘贴刚才复制的json文本,并将自己git的地址写好,保存即可 完整配置 { "workbench.colorTheme": ...

  2. HDU4460-Friend Chains-BFS+bitset优化

    bfs的时候用bitset优化一下. 水题 #include <cstdio> #include <cstring> #include <algorithm> #i ...

  3. Matplotlib学习---用matplotlib画折线图(line chart)

    这里利用Jake Vanderplas所著的<Python数据科学手册>一书中的数据,学习画图. 数据地址:https://raw.githubusercontent.com/jakevd ...

  4. 【BZOJ3809】Gty的二逼妹子序列 莫队 分块

    题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出区间\([l,r]\)有多少范围在\([a,b]\)的权值. \(n\leq 100000, ...

  5. 【BZOJ1211】【HNOI2004】树的计数 prufer序列

    题目描述 给你\(n\)和\(n\)个点的度数,问你有多少个满足度数要求的生成树. 无解输出\(0\).保证答案不超过\({10}^{17}\). \(n\leq 150\) 题解 考虑prufer序 ...

  6. ramdom 中的 seed 的使用

    实例 1 import ramdom # random.seed(10) # 未加 seed 的时候 for i in range(5): print(random.random()) # 每次输出结 ...

  7. mac上安装memcache

    1. 安装 brew (http://brew.sh/) /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/H ...

  8. luogu3119/bzoj3887 草鉴定 (tarjan缩点+spfa)

    首先缩一波点,就变成了一个DAG,边权是出点的大小 那我们走到某个点的时候可能会有两种状态:已经走过反边或者没走过 于是就把一个点拆成两层(x和x+N),第二层的点表示我已经走过反边了,每层中的边和原 ...

  9. CodeForces - 589D(暴力+模拟)

    题目链接:http://codeforces.com/problemset/problem/589/D 题目大意:给出n个人行走的开始时刻,开始时间和结束时间,求每个人分别能跟多少人相遇打招呼(每两人 ...

  10. limits.conf文件工作原理

    1. limits.conf 描述 limits.conf文件实际是Linux PAM(插入式认证模块,Pluggable Authentication Modules)中 pam_limits.so ...