9.2 day28

Breakpoint debugging

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
breakpoint should be added before being given the
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, the error code to put on, add breakpoints, breakpoint debugging

issubclass和isinstance

issubclass()

The second class is determined is not a subclass of the first class, returns true or false

ass Foo:
    pass
class Bar(Foo):
    pass

class Tt(Bar):
    pass
print(Bar.__bases__)
print(issubclass(Bar,Foo))   # true
print(issubclass(Tt,object))   # true

isinstance()

Analyzing the first parameter is not the object of the second parameter

class Foo:
    pass
class Tt():
    pass

f=Foo()
print(isinstance(f,Foo))    # true
print(isinstance(f,Tt))    # false

reflection

A user input a string, the string Chih heart corresponding 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 (): Set string attributes or methods
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往对象中赋值
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.name='lqz'
print(p.__dict__)
del p.name
print(p.__dict__)

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

#删除对象中属性为name字符串的属性


#判断一下对象中有没有没有我输入的属性,如果有,打印
p.name='lqz'
p.age=18
p.sex='male'
cmd=input('请输入要查询的属性:')
if hasattr(p,cmd):
    a=getattr(p,cmd)
    print(a)
else:
    print('该属性不存在')

example:

class BlackMedium:
    feature='Ugly'
    def __init__(self,name,addr):
        self.name=name
        self.addr=addr

    def sell_house(self):
        print('%s 黑中介卖房子啦,傻逼才买呢,但是谁能证明自己不傻逼' %self.name)
    def rent_house(self):
        print('%s 黑中介租房子啦,傻逼才租呢' %self.name)

b1=BlackMedium('万成置地','回龙观天露园')

print(b1.__dict__)
print(hasattr(b1,'sell_house'))
a='sell_house'
getattr(b1,'sell_house')
getattr(b1,a)()
sell_house=getattr(b1,a)
name=getattr(b1,'name')
print(name)
print(sell_house)

delattr(b1,'xxx')
print(b1.__dict__)
delattr(b1,'name')
print(b1.__dict__)

Modules are objects, these four methods may be used

Use their own modules to get through reflection Is there a way I'll use the module, if executed, would not prompt yet finished

Built-in method

Before we learned __init__ is the most simple built-in method

__str__

If you do not rewrite __str __, print will print out the memory address

_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)    # [nick]

__setattr__,__delattr__,__getattr__

These three points are the intercept method

If you go to fetch the object attributes, once to get any, will enter into __getattr_
If you assign attributes to objects, once to get any, will enter into __setattr_

If the object attribute is removed, it will enter __delattr__

class Foo:
    def __init__(self,name):
        self.name=name
    # def __getattr__(self, item):
    #     # print('xxxx')
    #     return '你傻逼啊,没有这个字段'
    # def __setattr__(self, key, value):
    #     print('yyyyy')
    def __delattr__(self, item):
        print('zzzzz')

f=Foo('nick')
# print(f.name)
# # print(f.age)
print(f.__dict__)
# print(f.name)
# f.sex='male'

del f.name
print(f.__dict__)

__item__ series

When the object 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)
        # print(name)
        # print(self.__dict__[item])
        return name
        # return self.__dict__[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)

f=Foo('nick')
print(f['name'])

__call__

Plus object calls

lass Foo:
    def __call__(self):
        print('xxxx')

f=Foo()
f()

__inter__ and __exit__

Context Manager

class Open:
def __init__(self, name):
self.name = name
def __enter__(self):
print('出现with语句,对象的__enter__被触发,有返回值则赋值给as声明的变量')
# return self
def __exit__(self, exc_type, exc_val, exc_tb):
print('with中代码块执行完毕时执行我啊')
with Open('a.txt') as f:
print('=====>执行代码块')

# print(f,f.name)

Guess you like

Origin www.cnblogs.com/hyc123/p/11449287.html