1、isinstance ,type, issubclass
 
      isinstance:判断给的对象是否是**类型
   
      type:返回**对象的数据类型
 
      issubclass:判断**类是否**的子类
class Animal:
def eat(self):
print('动物的世界你不懂') class Cat(Animal):
def play(self):
print('毛霸王') c = Cat() print(isinstance(c,Cat)) # True
# 向上判断
print(isinstance(c,Animal)) # True a = Animal()
print(isinstance(a,Cat)) # False # 不能向下判断 print(type(a)) # <class '__main__.Animal'>
print(type([])) # <class 'list'>
print(type(c)) # <class '__main__.Cat'> # 判断 **类是否是**类的子类
print(issubclass(Cat,Animal)) # True
print(issubclass(Animal,Cat)) # False
def cul(a,b):

    if (type(a) == int or type(a) == float) and (type(b) == int or type(b) == float):
return a+b
else:
print('提供的数据无法计算') print(cul(6,7))
2、如何区分方法和函数
 
  打印结果中包含了function 的为函数,包含method为方法
 
def func():
print('我是函数') class Foo:
def eat(self):
print('要吃饭了') print(func) # <function func at 0x0000020EDE211E18>
f = Foo()
f.eat() print(f.eat) # <bound method Foo.eat of <__main__.Foo object at 0x00000237390BE358>>
在类中:
        实例方法
            如果是类名.方法   则为函数
            如果是对象.方法   则为方法
 
           类方法:都是方法
            静态方法:都是函数
# 类也是对象
# 这个对象:属性就是类变量
# 方法就是类方法 class Person():
def eat(self):
print('明天去吃鱼') @classmethod
def he(cls):
print('我是类方法') @staticmethod
def lang():
print('我是静态方法') p = Person()
Person.eat(1) # 不符合面向对象的思维 print(p.eat) # <bound method Person.eat of <__main__.Person object at 0x00000253E992E3C8>>
print(Person.eat) # <function Person.eat at 0x00000253E992DF28> # 类方法都是方法
print(Person.he) # <bound method Person.he of <class '__main__.Person'>>
print(p.he) # <bound method Person.he of <class '__main__.Person'>> # 静态方法都是函数
print(Person.lang) # <function Person.lang at 0x0000024C942F1158>
print(p.lang) # <function Person.lang at 0x0000024C942F1158>
  from types import MethodType,FunctionType
    isinstance()
 
3、反射
 
    attr:attributebute
 
    getattr()
        从**对象获取到**属性值
 
    hasattr()
        判断**对象中是否有**属性值
 
    delattr()
        从**对象中删除**属性
 
    setattr()
        设置***对象中的**属性为**值
 
master.py
def eat():
print('吃遍人间美味') def travel():
print('游遍万水千山') def see():
print('看遍人间繁华') def play():
print('玩玩玩') def learn():
print('好好学习天天向上')
import master
while 1:
content = input('请输入你要测试的功能:')
# 判断 XXX对象中是否包含了xxx if hasattr(master,content):
s = getattr(master,content)
s()
print('有这个功能')
else:
print('没有这个功能')
class Foo:
def drink(self):
print('要少喝酒') f = Foo()
s = (getattr(f,'drink')) # 从对象中获取到str属性的值
s()
print(hasattr(f,'eat')) # 判断对象中是否包含str属性
setattr(f,'eat','西瓜') # 把对象中的str属性设置为value
print(f.eat)
setattr(f,'eat',lambda x:x+1)
print(f.eat(3))
print(f.eat) # 把str属性从对象中删除
delattr(f,"eat")
print(hasattr(f,'eat'))

python -- 面向对象 - 反射的更多相关文章

  1. python面向对象 : 反射和内置方法

    一. 反射 1. isinstance()和issubclass() isinstance( 对象名, 类名) : 判断对象所属关系,包括父类  (注:type(对象名) is 类名 : 判断对象所属 ...

  2. python面向对象反射-框架原理-动态导入-元类-自定义类-单例模式-项目的生命周期-05

    反射 reflect 反射(reflect)其实是反省,自省的意思 反省:指的是一个对象应该具备可以检测.修改.增加自身属性的能力 反射:通过字符串获取对象或者类的属性,进行操作 设计框架时需要通过反 ...

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

    一.反射 什么是反射:可以用字符串的方式去访问对象的属性,调用对象的方法(但是不能去访问方法),python中一切皆对象,都可以使用放射. 反射的四种方法: hasattr:hasattr(objec ...

  4. python面向对象(反射)(四)

    1. isinstance, type, issubclass isinstance: 判断你给对象是否是xx类型的. (向上判断 type: 返回xxx对象的数据类型 issubclass: 判断x ...

  5. python面向对象--反射机制

    class Black: feture="ugly" def __init__(self,name,addr): self.addr=addr self.name=name def ...

  6. Python 面向对象之反射

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

  7. Python面向对象之-反射

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

  8. python 面向对象编程 之 反射

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

  9. Python之面向对象-反射

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

随机推荐

  1. python框架之Django(7)-Cookie&Session使用

    Cookie 添加 response.set_cookie 添加明文cookie response.set_cookie(key, value='', max_age=None, expires=No ...

  2. Golang--匿名变量

    在使用多重赋值时,如果不需要在左值中接收变量,可以使用匿名变量(anonymous variable). 匿名变量的表现是一个下画线_,使用匿名变量时,只需要在变量声明的地方使用下画线替换即可.例如: ...

  3. 归并排序(Python实现)

    目录 1. 归并排序--while版本 2. 测试用例 3. 算法时间复杂度分析 1. 归并排序--while版本 def merge_sort_while(b_list): '''归并排序--whi ...

  4. 【LeetCode每天一题】Jump Game(跳跃游戏)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. 53.CSS---CSS水平垂直居中常见方法总结

    CSS水平垂直居中常见方法总结 1.元素水平居中 当然最好使的是: margin: 0 auto; 居中不好使的原因: 1.元素没有设置宽度,没有宽度怎么居中嘛! 2.设置了宽度依然不好使,你设置的是 ...

  6. Delphi中的消息 (转载)

    消息是Windows发出的一个通知,它告诉应用程序某个事件发生了.在Delphi中,大多数情况下Windows的消息被封装在VCL的事件中,我们只需处理相应的VCL事件就可以了,但如果我们需要编写自己 ...

  7. scrapy item pipeline

    item pipeline process_item(self, item, spider) #这个是所有pipeline都必须要有的方法在这个方法下再继续编辑具体怎么处理 另可以添加别的方法 ope ...

  8. Kubernetes应用健康检查

    目录贴:Kubernetes学习系列 在实际生产环境中,想要使得开发的应用程序完全没有bug,在任何时候都运行正常,几乎 是不可能的任务.因此,我们需要一套管理系统,来对用户的应用程序执行周期性的健康 ...

  9. VKD224B触摸芯片调试笔记

    1.通过阅读datasheet了解芯片怎么使用,一般datasheet会提供参考电路.和相应的电气参数. 通过上面的表格可以知道芯片的供电,所需电流. 这个芯片可以通过引脚选择模式.通过上面的选项设置 ...

  10. c# System.Object类和数据的安全转型

    .NET Fraework 最重要的引用类型之一是System命名空间中Object类.所有的类都是System.Object的派生类.System.Object类型的变量System.Object的 ...