Day 23 (Reflection)

1. Reflection

hasattr、getattr

These two are generally used together. Its usage is getattr(a,'asd'), the former is the class name (location), the latter must be a string type, and the usage of hasattr is consistent with it. The difference is that hasattr detects whether there is this method or attribute, while getattr executes it.

The use of gatattr is very wide, because the parameters behind it are of the type of spoof strings, which can be used with input, which greatly enhances the flexibility of the program

There are also two less commonly used setattr and delattr,

The former has 3 parameters, (where is it, what is it called, what is it changed to), it is used to create or modify variables, and it may be used occasionally

The latter has 2 parameters, (where, what is the name), used for deletion, very rarely used

2、isinstance 与  issubclass

Similar to type is a judgment type, the return value is bool, but there are differences.

If isinstance can determine the inheritance relationship, type cannot. The usage is isinstance(a, b)

issubclass can determine the inheritance relationship, but it must be issubclass (subclass, parent class) to return True

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324537524&siteId=291194637