Twenty II

A breakpoint debugging

A breakpoint is a signal that tells the debugger to temporarily suspend the implementation of the program on a specific point. When performing hangs a breakpoint, we call the program in break mode. Enter break mode does not terminate or end the program. You can continue to perform at any time.

Breakpoint provides a powerful tool that enables you to suspend execution time and desired location. And sentence by sentence or instruction by instruction to check the code is different, you can let the program execute until a breakpoint is encountered, and then start debugging. This greatly speeds up the debugging process. Without this feature, large debugger is almost impossible.

Instructions:

  1. Where you want to add breakpoint with a mouse click, it will turn red to the implementation of this program, you will be stuck in this (if it is gray, to the implementation of this program, will not stop).
  2. Click on the down arrow (step over): Stepping (program step by step).
  3. On the left there is a green arrow: quickly jump to the next breakpoint.
  4. Console error when we click on the error message, the row will return to the wrong location, and if there are changes but did not re-run, an error message or the reality of the row. Breakpoint location is generally added to the last row of the error flag information, or the upper row.

Two, isinstance and issubclass

2.1 isinstance

Analyzing the first class is a subclass of the second class not, returns true or false.

class Foo:
    pass
class Bar(Foo):
    pass

class Tt(Bar):
    pass
print(issubclass(Bar,Foo))
print(issubclass(Foo,Bar))

True
False

2.2 issubclass

Analyzing 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))

True
False

Third, reflection

Attribute operations is reflected by the object class or string.

It is simply reflected by a string, import module; by a string, to find the specified function module, and execute. Use a string to an object (module) in operation (Find / get / delete / add) members, based on a string of driving events!

Essentially reflected in the use of built-in functions, wherein the reflector has four built-in functions:

  1. hasattr: to determine whether the target object contains properties named name (hasattr by calling getattr (ojbect, name) throws an exception if implemented).

  2. getattr: to acquire memory address corresponding to the method according to the string in the object obj, added "()" to perform the brackets.

class Foo(object):
    def __init__(self,name):
        self.name = name
obj = Foo('xxx')
#获取变量
v1 = getattr(obj,'name')
#获取方法
method_name = getattr(obj,'login')
method_name()
  1. setattr: This is the corresponding getattr (). Parameter is an object, a string and an arbitrary value. The string may be listed in an existing property or a new property. This function assigns a value to the property. This object allows it provides. For example, setattr (x, "foobar", 123) corresponding to x.foobar = 123.

  2. delattr: a set of functions related setattr (). Parameter is an object (remember python, everything is an object) and a string composed. The string argument must be one of the object property name. This function deletes a specified by the obj string attribute.

r = hasattr(commons, xxx)  # 判断某个函数或者变量是否存在
print(r)

setattr(commons, 'age', 18)  # 给commons模块增加一个全局变量age = 18,创建成功返回none

setattr(commons, 'age', lambda a: a + 1)  # 给模块添加一个函数

delattr(commons, 'age')  # 删除模块中某个变量或者函数

Fourth, the method of the built-in class

4.1 str

String display object change. Can be understood as when using the print function to print an object it will automatically call __str__ method of the object.

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    # 定义对象的字符串表示
    def __str__(self):
        return self.name
    
s1 = Student('张三', 24)
print(s1)  # 会调用s1的__str__方法

4.2 repr

In the python interpreter environment, it will represent the default display object repr.

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    # 定义对象的字符串表示
    def __str__(self):
        return self.name
    
s1 = Student('张三', 24)
print(s1)  # 会调用s1的__str__方法

4.3 call

__Call__ method performed by the object triggers the brackets, namely: Object (). Objects with this method can be called like a function.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __call__(self, *args, **kwargs):
        print('调用对象的__call__方法')

a = Person('张三', 24)  # 类Person可调用
a()  # 对象a可以调用

4.4 hasattr,getattr,setattr,delattr

Guess you like

Origin www.cnblogs.com/tangceng/p/11449149.html
ii