zoukankan      html  css  js  c++  java
  • python反射

    isinstance: 判断对象是否是属于这个类(向上判断)
    type: 返回某对象的数据类型
    issubclass: 判断某类是否是这个类的子类

    class Animal:
        def chi(self):
            print('吃饭')
    class Cat(Animal):
        def he(self):
            print('喝水')
    c = Cat()
    print(isinstance(c,Cat))   #判断c是否是Cat的对象
    print(isinstance(c,Animal)) #判断c是否是Animal的对象,只能向上判断
    a = Animal()
    print(isinstance(a,Cat))   #不能向下判断
    
    精准的判断这个对象属于哪个类
    print(type(a))
    print(type(c))
    
    判断某类是否是这个类的子类
    print(issubclass(Cat,Animal))
    print(issubclass(Animal,Cat))
    结果
    True
    True
    False
    <class '__main__.Animal'>
    <class '__main__.Cat'>
    True
    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:
            return '无法计算'
    print(cul('sad',13))
    结果
    无法计算

    区分方法和函数(代码)
    野路子: 打印的结果中包含了function的是函数,包含method的是方法

    def func():
        print('我是函数')
    class Foo:
        def chi(self):
            print('吃饭')
    print(func)
    f = Foo()
    f.chi()
    print(f.chi)
    结果
    <function func at 0x0000025A57361E18>
    吃饭
    <bound method Foo.chi of <__main__.Foo object at 0x0000025A590573C8>>

    在类中:(类也是对象)
    实例方法
        如果是类名.方法  函数
        如果是对象.方法  方法
    类方法: 都是方法
    静态方法: 都是函数

    class Person:
        def chi(self):
            print('我是实例方法')
        @classmethod
        def he(cls):
            print('我是类方法')
        @staticmethod
        def wa():
            print('我是静态方法')
    p = Person()
    print(p.chi)
    Person.chi(1)       # 不符合面向对象的思维(不建议用)
    print(Person.chi)
    
    类方法: 都是方法
    print(Person.he)
    print(p.he)
    
    静态方法: 都是函数
    print(Person.wa)
    print(p.wa)
    结果
    <bound method Person.chi of <__main__.Person object at 0x000002437C2B73C8>>
    我是实例方法
    <function Person.chi at 0x000002437C2B98C8>
    <bound method Person.he of <class '__main__.Person'>>
    <bound method Person.he of <class '__main__.Person'>>
    <function Person.wa at 0x000002437C2B99D8>
    <function Person.wa at 0x000002437C2B99D8>

    判断是否是方法或函数

    from types import MethodType, FunctionType
    isinstance()
    
    from types import FunctionType,MethodType
    class Person:
        def chi(self):
            print('我是实例方法')
        @classmethod
        def he(cls):
            print('我是类方法')
        @staticmethod
        def wa():
            print('我是静态方法')
    p = Person()
    print(isinstance(Person.chi,FunctionType))
    print(isinstance(p.chi,MethodType))
    
    print(isinstance(p.he,MethodType))
    print(isinstance(Person.he,MethodType))
    
    print(isinstance(p.wa,FunctionType))
    print(isinstance(Person.wa,FunctionType))
    结果
    True
    True
    True
    True
    True
    True

    反射
    对于模块而言可以使用getattr,hasattr,同样对于对象也可以执行类似的操作
    getattr(): 从某对象中获取到某属性值
    hasattr(): 判断某对象中是否有某属性值

    在一个文件中写一个py文件
    def chi():
        print('吃饭')
    def he():
        print('喝水')
    
    再在另外一个文件中判断
    import test2
    while 1:
        content = input("<<:")
        if hasattr(test2,content):
            print('有这个功能')
            ret = getattr(test2,content)
            ret()
        else:
            print('没有这个功能')
    结果
    <<:chi
    有这个功能
    吃饭
    <<:hh
    没有这个功能

    delattr(): 从某对象中删除某个属性
    setattr(): 设置某对象中的某个属性为xxx

    class Person:
        def __init__(self,name,addr):
            self.name = name
            self.addr = addr
    p = Person('jack','北京')
    print(hasattr(p,'addr'))    #判断这个属性是否存在
    print(getattr(p,'addr'))    #打印属性值
    setattr(p,'addr','上海')    #修改属性值p.addr='上海'
    setattr(p,'money',9999999)  #相当于新添加一个属性money=999999
    print(p.addr)
    print(p.money)
    delattr(p,'addr')
    # print(p.addr)     #因为addr这个属性被删除了,所以打印报错
    结果
    True
    北京
    上海
    9999999
  • 相关阅读:
    jQuery插件开发——元素拖拽和基于鼠标位置为中心缩放
    基于Quartz.Net的任务管理平台开发(3) —— 任务管理平台
    记录一下Linq实现多列排序的问题
    动态构造Lambda表达式
    MVC全局实现异常处理
    修改自增列起始值,仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'******'中的标识列指定显式值
    统计指定时间段内生日的用户
    SQL开关功能记录
    枚举对象实现 DropDownList 的转换操作二
    枚举对象实现 DropDownList 的转换操作一
  • 原文地址:https://www.cnblogs.com/wangm-0824/p/10199623.html
Copyright © 2011-2022 走看看