Bound and unbound method and reflection, isinstance built-in functions and issubclass

Binding and non-binding approach method

1. binding method

Binding method: bind to whoever should call Who, who will call who will be the first argument as a method to bind to the object: class defined in the default function is to bind object binding method to the class: class defined functions plus a decorator classmethod




2. Non-binding method (staticmethod)

    非绑定方法: 既不与类绑定,又不与对象绑定,意味着对象和类都可以来调用,无   论谁来调用都是一个普通的函数,没有自动传值的效果;为类中定义的函数加上一个装  饰器staticmethod。

Examples

class Foo:
    def f1(self):
        print(self)

    @classmethod
    def f2(cls):
        print(cls)   # <class '__main__.Foo'>


    @staticmethod
    def f3(x,y):
        print('f3',x+y)


obj=Foo()
print(Foo.f1)  # 就是一个普通函数  <function Foo.f1 at 0x0000000002965620>
print(obj.f1)  # 对象绑定方法  <bound method Foo.f1 of <__main__.Foo object at 0x0000000009F83C88>>
obj.f1()  # <__main__.Foo object at 0x0000000009F83C88>

print(Foo.f2)  # 类绑定方法  <bound method Foo.f2 of <class '__main__.Foo'>>
Foo.f2()
print(obj.f2)  # <bound method Foo.f2 of <class '__main__.Foo'>>
obj.f2()

print(Foo.f3)  # <function Foo.f3 at 0x0000000009FF4F28>
print(obj.f3)  # <function Foo.f3 at 0x0000000009FF4F28>

Foo.f3(1,2)  # f3 3
obj.f3(3,4)  # f3 7

application

# settings.py
ip='127.0.o.1'
port='3306'

# 当前文件
import settings
class MySql:
    def __init__(self, ip, port):
        self.id = self.create_id()
        self.ip = ip
        self.port = port

    def tell_info(self):
        print('<id:%s ip:%s port:%s>' % (self.id, self.ip, self.port))

    @classmethod
    def from_conf(cls):
        return cls(settings.IP, settings.PORT)

    @staticmethod
    def create_id():
        import uuid
        return uuid.uuid4()



# obj1=MySql('1.1.1.1',3306)
# obj1.tell_info()  # <id:9b13ad59-10e0-4b65-af15-b5d7d97034e1 ip:1.1.1.1 port:3306>
obj2 = MySql.from_conf()
obj2.tell_info()

isinstance built-in functions and issubclass

1.isinstance

Determining whether an object is an instance of a class.
the isinstance (parameter 1, parameter 2): a parameter for determining whether an instance of the parameter 2.
the isinstance (object class)

2.issubclass

Determining whether a class is a subclass of another class
issubclass (parameter 1, parameter 2): parameter 1 is determined by whether the parameter is 2 subclass.

print(isinstance(10,int))  # True

class Foo:
    pass
class Goo(Foo):
    pass

foo_obj = Foo()
print(isinstance(foo_obj, Foo)) # True
print(isinstance(foo_obj, Goo))  # False
print(issubclass(Goo, Foo))  # True

foo_obj = Goo()
print(isinstance(foo_obj, Goo)) # True
print(isinstance(foo_obj, Foo))  # True

reflection

Definition of reflection

Reflection is reflection, introspection meaning, referring to an object to be provided, can be detected, modify, increase the capacity of their own attributes; the form of the operation target character string attributes associated.

4 can be achieved reflection function (built-in function)

the hasattr (Parameter 1 (object), the parameter 2) determines whether an object exists a property;
getattr (Parameter 1 (object), parameter 2, parameter 3) removing a property from a parameter, the third parameter is the default value, when the attribute does not exist, the third parameter is not given, so, returns the third parameter;
setattr (parameter 1 (object), 2 parameters (attributes), 3 parameters (attribute value)), Add a new property to an object
delattr (parameter 1 (Object), parameter 2 (attribute name)): delete the property from the object;

class People:
    country = 'China'

    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex

p = People('tank', 17, 'male')

# 普通方式
print('name' in p.__dict__)  # True
print('country' in People.__dict__)  # True
print('country2' in People.__dict__)  # False
# hasattr
print(hasattr(p, 'name'))  # True
print(hasattr(People, 'country'))  # True

# 普通方式
print(p.__dict__.get('name'))  # tank
print(p.__dict__.get('level', '9'))  # 9

# getattr
print(getattr(p, 'name', 'jason_sb'))  # tank
print(getattr(p, 'name1', 'jason_sb'))  # jason_sb
print(getattr(People, 'country2', 'China'))


# setattr
# 普通
p.level = 10
print(p.__dict__) # {'name': 'tank', 'age': 17, 'sex': 'male', 'level': 10}

# 反射
setattr(p, 'sal', '3.0')
print(hasattr(p, 'sal'))  # True
print(p.__dict__)  # {'name': 'tank', 'age': 17, 'sex': 'male', 'level': 10, 'sal': '3.0'}

# delattr
# 普通
del p.level
print(hasattr(p, 'level'))  # False

# 反射
delattr(p, 'sal')
print(hasattr(p, 'sal'))  # False

Small reflective practice

class Movie:
    def input_cmd(self):
        print('输入命令:')
        while True:
            cmd = input('请输入执行的方法名:').strip()

            # 若用户输入的cmd命令是当前对象的属性
            if hasattr(self, cmd):
           # if cmd in self.__class__.__dict__:
                method = getattr(self, cmd)
                method()
            else:
                print('命令错误,请重新输入!')

    def upload(self):
        print('电影开始上传...')

    def download(self):
        print('电影开始下载...')

movie_obj = Movie()
movie_obj.input_cmd()

Reflecting usage scenarios

Reflection of the fact that the property additions and deletions to change search, but if you use the built-in direct __dict__ to operate, grammar cumbersome, difficult to understand

Another major problem is that if the object is not my own writing is the other party, I must determine whether the object is to meet the requirements, whether I needed properties and methods

Application: Generally used in frame design, reflection is known as the cornerstone of the frame; after because the designers framework, it is impossible to know in advance how your objects in the end is designed so that you provided to the object framework, it must be verified in order to determine normal use, authentication is determined to do is reflected, of course, it can be achieved by __dict__ are, in fact, the operation of these methods is carried __dict__ package

Dynamic reflection import example

'''
需求:要实现一个用于处理用户的终端指令的小框架
框架就是已经实现了最基础的构架,就是所有项目都一样的部分
'''
# libs.plugins.py  # libs文件夹下的plugins文件

# 插件部分
class WinCMD:

    def cd(self):
        print("wincmd 切换目录....")

    def delete(self):
        print("wincmd 要不要删库跑路?")

    def dir(self):
        print("wincmd 列出所有文件....")

class LinuxCMD:

    def cd(self):
        print("Linuxcmd 切换目录....")

    def rm(self):
        print("Linuxcmd 要不要删库跑路?")

    def ls(self):
        print("Linuxcmd 列出所有文件....")

# settings.py 文件
'''作为框架的配置文件'''
# 作为框架使用者,在配置文件中指定你配合框架的类是哪个框架的使用者提供一个配置文件,要求对方将类的信息写入配置文件,然后框架自己去加载需要的模块

    class_path = 'libs.plugins.WinCMD'  # libs是plugins.py文件所在的文件夹

# myframework.py 文件
#框架的设计者
import importlib
import settings
# 框架已经实现的部分
def run(plugin):
    while True:
        cmd = input("请输入指令:")
        if cmd == "exit":
            break
        # 因为无法确定框架使用者是否传入正确的对象所以需要使用反射来检测
        # 判断对象是否具备处理这个指令的方法
        if hasattr(plugin,cmd):
            # 取出对应方法方法
            func = getattr(plugin,cmd)
            func() # 执行方法处理指令
        else:
            print("该指令不受支持...")
    print("see you la la!")

# 创建一个插件对象 调用框架来使用它
# wincmd = plugins.WinCMD()
# 框架之外的部分就有自定义对象来完成

# 框架得根据配置文件拿到需要的类
path = settings.CLASS_PATH
# 从配置中单独拿出来 模块路径和 类名称
module_path,class_name = path.rsplit(".",1)  # libs.plugins,WinCMD
#拿到模块
mk = importlib.import_module(module_path)
# 拿到类
cls = getattr(mk,class_name)
# 实例化对象
obj = cls()
#调用框架
run(obj)

Guess you like

Origin www.cnblogs.com/zhangchaocoming/p/11669094.html