Python attribute search and python attributes
Execute obj. the special method obj. _ getattribute _ ('attr '). This method is used to search for this attribute. It usually involves checking features, searching for instance dictionaries, searching for class dictionaries, and searching for base classes. If the search process fails, the _ getattr _ () method of the class is finally called. If this fails, an AttributeError error is thrown.
The procedure is as follows:
1. If attr is a special attribute (for example, provided by python), it will be returned directly.
2. in obj. _ class __. _ dict _ is the attr in the class dictionary. If attr is a data descriptor, the _ get _ Method Result of the data descriptor is returned. If no, search for the data descriptor in the base class of obj. _ class. Note that the priority of the non-data descriptor that only implements the _ get _ method is later than that of the data descriptor.
3. Search in the obj. _ dict _ instance dictionary and return the result directly. If obj is a class, search for it in the _ dict _ of obj and its base class in sequence, if a descriptor is found, the result of the _ get _ method of the descriptor is returned. Otherwise, the attr is returned directly. If not, proceed to the next step.
4. in obj. _ class __. _ dict _ is a dictionary-like query. If a non-data descriptor is found, the result of the _ get _ method of the descriptor is returned. If a common property is found, the property value is directly returned. Perform the same search in the obj base class.
5. Raise AttributeError
In general, first check the data descriptor of the object, then check the instance dictionary, check the non-data descriptor of the class and base classes, and finally the class dictionary and the base class dictionary.
Check whether a tag exists in python.
Book. p. one. get ("age", None). If the value of age exists, get will return its value. If the value of age does not exist, None will be returned. Then add a judgment to know if there is any age.
You can also use the has_key function to determine whether a key value exists.
Search for the same string in python
'Sdfs'. count ('s ')