Use breakpoint debugging, issubclass and the ininstance

First-class citizen

As long as something can be assigned to a variable, this thing called first-class citizens

Breakpoint debugging

Where you want to add a breakpoint with a click of the mouse, you will see a red circle

Red place to execute the program will be suspended

Before power should be loaded error

Green arrow indicates the acceleration skip to the next breakpoint execution

Console error: Click on the last line you can read, the cursor will quickly locate the error code, add breakpoints in the code above error, breakpoint debugging

issubclass和isinstance

issubclass: determining a first class is not a child of the second class, returning 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,object))

ininstance: determining the first parameter is not the second object parameters, returns true or false

class Foo():
    pass
class Tt():
    pass
f=Foo()
print(isinstance(f,Foo))
print(isinstance(f,Tt))

reflection

A user input a string, the string corresponding to the implementation of the method

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

class People:
    country='China'
    def __init__(self,name):
        self.name=name

    def eat(self):
        print('%s is eating'% self.name)
peo=People('NICK')
print(hasattr(peo,'eat'))

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

class People:
    country='China'
    def __init__(self,name):
        self.name=name

    def eat(self):
        print('%s is eating'% self.name)
peo=People('NICK')
print(getattr(peo,'xxxx',None))

setattr () : method to set properties or string

class People:
    country='China'
    def __init__(self,name):
        self.name=name

    def eat(self):
        print('%s is eating'% self.name)
peo=People('NICK')
setattr(peo,'age',18)
print(peo.age)

delattr () : method to set properties or string

class People:
    country='China'
    def __init__(self,name):
        self.name=name

    def eat(self):
        print('%s is eating'% self.name)
peo=People('NICK')
delattr(peo,'name')
print(peo.__dict__)

Built-in method

_str _: If you do not rewrite __str__, print print out the memory address, if rewritten, it will print out what you want

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

__setattr__,__delattr__,__getattr__
点拦截方法
如果去对象中去属性,一旦取不到,会进入到__getattr__
如果去对象中赋值属性,一旦取不到,会进入__setattr__
如果删除对象中的属性,会进入到__delattr__

Write a class that inherits the dictionary, it can. Values, the values ​​can be in brackets

class Mydict(dict):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
    def__getattr__(self,item):
        return self[item]
    def __setattr__(self,key,value):
        self[key]=value
        
di=Mydict(name='lzs',age=18)
print(di['name'])
print(di.name)
di.sex='male'
di['sex']='male'

_item _ series, objects by [] brackets value assignment, delete the value when calls

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

_call _: Object brackets will call it

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

_enter _ and __exit__: Context Manager

with open() as f:
    pass

class Mydict():
    def__getattr__(self,item):
        print('xxxx')
        return '该属性不存在'
m=Mydict()

Guess you like

Origin www.cnblogs.com/lzss/p/11511236.html