Import the third-party library in the class, use the function in the class, and when the function is referenced outside the class, an error is reported: the library cannot be found (python namespace-scope)

1. Picture problem display

Situation one ( problems encountered in this article ):

operation result

Case two (the verification code is no problem):

operation result

Case three (the verification code is no problem):

operation result

Situation four (for verification):

operation result

Situation 5 (for comparison):

Why put five situations, because in order to test various conjectures, avoid the problem caused by other reasons.

2. Problem description

Import a third-party module into a class. The function defined in the class uses this module. When we call it externally, an error occurs: The module does not exist.

Three, the cause of the problem

1. Involving knowledge points: python scope and namespace;

2. We should know that the ordinary variable or function in the class or function is equivalent to a local variable (if it is not declared as a global variable), then it cannot be used directly outside the class or function. For example, the method in the class also needs to be used Call to use. Then the third-party library imported in this class is equivalent to importing many variables and functions of this third-party library inside this class, so it is also partial and cannot be used directly outside.

Four, the solution

1. Outside the class, import the module. That is, import before calling;

2. Import this module in this function.

3. In this function tool.module_name.function(). That is to access the members of the class.

4. Declare the attribute as a global attribute in the class

Five, principle analysis

1. Basic knowledge points of python scope and namespace:

1. Functions, classes and modules in python will have scope;

2. Namespace:

           1. Also known as namespace, it is the mapping from name to object. Different namespaces are created at different times and have different lifetimes;

           2. There is no connection between the naming in different namespaces;

           3. When a function is called, a local namespace is created for it;

           4. The class definition is also another namespace in the local scope.

3. Scope:

           1. The scope is a text area of ​​Python. In this area, the namespace can be "directly accessed". Direct access here refers to an attempt to find an absolute reference (unqualified reference) of the name in the namespace;

           2. Example: direct reference; directly use the name access method, such as name, this method tries to search for the name name in the name space.

                        Indirect reference; use the form of objname.attrname, that is, attribute reference. This method will not search for the name attrname in the namespace, but search for the name objname, and then access its attributes.

2. Conclusion

1. Classes, functions, and modules all generate their own namespaces, and they cannot be directly referenced between different namespaces.

    If you want to use it, you need to refer to it indirectly, or declare a certain attribute (variable, function, module) as a global namespace.

   As for the more in-depth research and discussion of scope and namespace, it is not within the scope of this article for the time being. If there is any need, please refer to 6. References. Very detailed.

Six, reference

1. Python Chinese documentation:  http://www.pythondoc.com/pythontutorial3/classes.html  ;

2. Advanced Python_About namespace and scope (detailed explanation)   https://www.jb51.net/article/114951.htm  ;

3. The above is very well written, for in-depth understanding and learning, you can go to research and study.

 

Seven, other instructions

1. Learn for yourself, not for others. This article is mainly used for self-study and record. If there is an error, please forgive me, and hope to point it out, thank you!

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_41914687/article/details/113407772