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框架之Flask(1)-Flask初使用

    Flask是一个基于 Python 开发并且依赖 jinja2 模板和 Werkzeug WSGI 服务的一个微型框架,对于 Werkzeug 本质是 Socket 服务端,其用于接收 http 请求 ...

  2. [Python] Print input and output in table

    Print the input and output in a table using prettyTable. from prettytable import PrettyTable import ...

  3. Qt 拷贝内容到粘贴板 || 获取粘贴板内容

    QString source = ui->textEdit_code->toPlainText(); QClipboard *clipboard = QApplication::clipb ...

  4. CentOS 7 MariaDB-MHA

    关于MHA    MHA(Master High Availability)是一款开源的mysql高可用程序,目前在mysql高可用方面是一个相对成熟的解决方案.MHA 搭建的前提是MySQL集群中已 ...

  5. Linux基础命令---cancel取消打印任务

    cancel cancel指令用来取消已经存在的打印任务. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.openSUSE.SUSE.   1.语法       ...

  6. Dom 兼容处理

    获取子节点:childNodes 在IE下是可以正常使用的 但是在FF包含了文本节点需要配合nodeType做个类型判断  1是元素节点  3是文本节点 也可以采用 children IE       ...

  7. 【题解】Luogu P2153 [SDOI2009]晨跑

    原题传送门 一眼应该就能看出是费用流 因为每个交叉路口只能通过一次,所以我们进行拆点,连一条流量为1费用为0的边 再按照题目给的边(是单向边)建图 跑一下MCMF就行了 拆点很套路的~ #includ ...

  8. D1图论最短路专题

    第一题:poj3660 其实是Floyed算法的拓展:Floyd-Wareshall.初始时,若两头牛关系确定则fij = 1. 对于一头牛若确定的关系=n-1,这说明这头牛的排名是确定的. 通过寻找 ...

  9. oracle的存储过程的作用

    oracle的存储过程的作用 1.存储过程可以使得程序执行效率更高.安全性更好,因为过程建立之后 已经编译并且储存到数据库,直接写sql就需要先分析再执行因此过程效率更高,直接写sql语句会带来安全性 ...

  10. JAVA中对字符串的常见处理函数汇总

    字符串 看到字符串,想到字符串处理中,有 字符串的反转,初级面试中常用到 字符串分割成字符串组,初级面试中常用到 字符串中的替换,初级面试中常用到 字符串中的截取,初级面试中常用到 反转reverse ...