High order reflection, magic class method day28

A, issubclass and isinstance

issubclass: determining the first parameter is not the second sub-type of parameter returns True or False

# issubclass

class Foo:
    pass
class Bar(Foo):
    pass
class Tt(Bar):
    pass
print(Bar.__bases__)
print(issubclass(Bar, Foo))
print(issubclass(Tt, objcet)

isinstance: determining the first parameter is not the second object parameters, return True or False

class Foo:
    pass
class Tt():
    pass

f = Foo()
print(isinstance(f, Foo))
print(isinstance(f, Tt))

Second, reflection

Reflection: is acquired by a string, setting, deleting object property or method

hasattr (): determining whether an attribute in an object, returns True or False

getattr (): get property methods or string, if acquired, it will return the corresponding property or method

setattr (): method to set properties or string

delattr (): method to delete an attribute or a string

class Foo:
    def run(self):
        print('run')
    def speak(self):
        print('speak')

p = Foo()
print(Foo.__dict__)
cmd = input('请输入命令:')

# 方案一:
print(Foo.__dict__[cmd])
Foo.__dict__[cmd](p)

# 方案二:
if hasattr(p, cmd):
    run = getattr(p, cmd)
    run()
else:
    print('该命令不存在')

# 通过用户输入key和value往对象中赋值
p = Foo()
key = input('请输入key:')
value = input('请输入value:')
setattr(p, key, value)
print(p.age)

# 动态的往对象中放方法
def test(a):
    print(a)
print(p.__dict__)
setattr(p, 'test', test)
print(p.__dict__)
p.test(0)

# 动态删除p中属性为变量a的属性
p.name = 'lqz'
p.age = 19
p.sex = 'male'
a = input('请输入要删除的属性:')
print(p.__dict__)
delattr(p, a)
print(p.__dict__)

Third, the built-in method

__str__: Rewrite print after print is __str__ return value without rewriting the return of the memory address of the object

__repr__ similar with str, direct write the variable name in an interactive command, execute __repr__

class Foo:
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return self.name

f = Foo('nick')
# print(f.__str__())
print(f)  # 相当于上面那句

__setattr__, __delattr__, __getattr__

Point intercept method:

If you go to fetch the object attributes, once to get any, will enter into __getattr__

If you object to assign attributes (add or modify), will enter into __setattr__

If the attribute deleted object, will enter __delattr__

class Foo:
    def __init__(self, name):
        self.name = name
    def __getattr__(self, item):
        return '你傻逼阿, 没有这个字段'
    def __setattr__(self, key, value):
        # self.key = value   无限递归
        self.__dict__[key] = value
    def __delattr__(self, item):
        # del self.item    无限递归
        self.__dict__.pop(item)
f1 = Foo(10)

When __item__ series of objects by [] brackets value assignment, remove the value of calls

class Foo:
    def __init__(self, name):
        self.name = name
    def __getitem__(self, item):
        name = getattr(self, item)
    def __setitem__(self, key, value):
        print('obj[key]=lqz赋值时,执行我')
        self.__dict__[key] = value
    def __delitem__(self, key):
         print('del obj[key]时,执行我')
         self.__dict__.pop(key)

__call__: Object brackets will call it

class Foo:
    def __call__(self):
        print('xxxx')
        
f = Foo()
f()

__enter__ and __exit__

Context Manager

Fourth, breakpoint debugging

First-class citizens: as long as it can be assigned to a variable thing, this thing called first-class citizens

Where you want to add breakpoint with a mouse click it, you'll see a red circle
where the red, to the implementation of the program will be suspended
before the power failure should be loaded error
green arrow indicates quickly jump to a breakpoint execution

Console error, click on the last line you can read, the cursor will quickly locate the error code, put on the error code, plus power, breakpoint debugging

Guess you like

Origin www.cnblogs.com/17vv/p/11448644.html