
Before starting the introduction of classes, I would like to talk about python scope and namespace.
The namespace is a mapping from name to object. Essentially, it is implemented by python dictionary.
The search order is based on LEGB rule, that is local namespace, enclosing function, global namespace and build-in namespace; then if not found, raise a NameError.
nonlocal and global
Usage of nonlocal and global:
def (): |
Result:
After normal: hello, the first line.
After NonlocalEx: nonlocal variable
After GlobalEx: nonlocal variable
In Top-Level: global value
In order to see the effect depth of the nonlocal and global, I tried the following code:
def (): |
Result:
After normal: hello, the first line.
After subnormal: the sub line.
After subNonlocalEx: subnonlocal variable
After subGlobalEx: subnonlocal variable
After NonlocalEx: subnonlocal variable
After GlobalEx: subnonlocal variable
In Top-Level: subglobal value
But if I uncomment the globalEx(), the result of print("In Top-Level:", t) is “In Top-Level: global value”, which means the most outside global statement effect the final global value. While for nonlocal the innermost nonlocal statement affects the given object value.
Note, for the same level or depth of the nonlocal or global declaration, the last time call is in effect. Besides, the nonlocal variables only effect the closure scope not the module scope, which is effected by global keyword.
classes
In classes, instancename.method is a method object, and classname.method is a function object. The call instanceName.f() is exactly equivalent to MyClass.f(instanceName). Instance objects can only understand attribute references. There are two kinds of valid attribute names, data attributes(which called data members in C++) and methods.
Method calling convention: ‘When an instance attribute is referenced that isn’t a data attribute, its class is searched. If the name denotes a valid class attribute that is a function object, a method object is created by packing (pointers to) the instance object and the function object just found together in an abstract object: this is the method object. When the method object is called with an argument list, a new argument list is constructed from the instance object and the argument list, and the function object is called with this new argument list.’
‘(For C++ programmers: all methods in Python are effectively virtual.)’
Useful built-in functions to check the inheritance relation in classes: isinstance(objName, className), issubclass(sub, Base).
Multiple Inheritance
Search rule(python3.7): “depth-first, left-to-right, not searching twice in the same class where there is an overlap in the hierarchy.” Also called method resolution order, which changes dynamically to support cooperative calls to super().
In addition, for new-style classes, the C3 Linearization algorithm is added used. For more detail, you can reach method-resolution-order.
For example:
class A(object): |
The Result is:
A |
Here, accoding to the result of d.mro(), when I call d.go(), it in turn calls B.go(), which calls C.go(), which calls A.go(). Once this reaches the top of the diamond, all of the same name methods actually do their work in the opposite order from how their go functions were called.
Additionally
The eval() function can only execute Python expressions, while the exec() function can execute any valid Python code.virtualenv is used to figure out the conflict between different package versions.




近期评论