Analysis and Interpretation of Python applications

Source: Internet
Author: User
Tags export class

Python application is a real object-oriented language, it only adds a few new syntax to implement the class, its class mechanism is the combination of C ++ and Modula-3 class mechanism, the Python class does not strictly restrict the modification of the definition. It relies on the user's conscious not to modify the definition.

The class inheritance mechanism allows inheritance of multiple base classes. The export class can overload any method of the base class, and the method can call the method of the same name of the base class. Objects can contain any amount of private data. In terms of C ++, all class members, including data members, are public and all member functions are virtual. There is no particular build function or destroy function (destructor ).

As in the Modula-3, there is no simple way to reference object members from an object's method: The method function must take the object as the first parameter, which is automatically provided during calls. Like in Smalltalk, classes are also objects. In fact, the meaning of objects is relatively wide: All data types in Python are objects.

As in C ++ or Modula-3, built-in types cannot be extended by users as base classes. And, like C ++ but not like Modula-3, Most built-in functions with special syntax such as arithmetic operators, subscript, etc.) can be redefined as class members.

Python applications have a wide range of object concepts, and objects do not have to be class instances, because Python data types are not all classes, just like C ++ and Modula-3, unlike Smalltalk. For example, the basic built-in integer and list types are not classes, or even the odd types such as files are not classes. However, all data types in Python are more or less subject to object-like syntaxes.

An object has a separate identity. The same object can have multiple names associated with it. This is called an alias in other languages. The benefits of doing so are not obvious at first glance, and there is no difference in non-variable types (numbers, strings, orders (tuple.

However, the alias syntax affects programs that contain variable objects such as lists, dictionaries, and external objects such as files and windows, which facilitates programming. Before introducing a class, we must talk about the Python scope rules. The class definition makes good use of the namespace. You Need To Know How Python handles the scope and namespace to fully understand the use of the class. In addition, the scope rules are also a must-have knowledge for advanced Python programmers.

It is easy to pass an object because the alias is similar to a pointer. If a function modifies an object passed as a parameter, the Modification result can be used for callback. In this way, you do not need to use two parameter transfer mechanisms as Pascal does.

Namespace is the ing from name to object. Most namespaces are currently implemented using the Python dictionary type, but these 1.1 are not noticed and may change in the future. The following are examples of namespaces: built-in functions such as abs in Python, and built-in exception names), Global names in the module, and local variable names in function calls.

In a sense, all attributes of an object also constitute a namespace. The most important thing about namespaces is to know that the names of different namespaces are irrelevant. For example, two different modules may define a function called "maximize" without confusion, because the Module User must add the module name before the function name.

In addition, any name after a period can be called an attribute in a Python application. For example, in the expression z. real, real is the attribute of an object z. Strictly speaking, a reference to a module name is a Property Reference: In the expression modname. funcname, modname is a module object, and funcname is an attribute.

In this case, there is a direct ing between the module attribute and the global name defined by the module: they use the same namespace! The attribute can be read-only or writable. You can assign values to attributes when they are writable.

Namespaces are created at different times and have different lifecycles. A namespace containing the Python built-in name is created and not deleted when the Python interpreter starts. The global namespace of a module is created when the module definition is read. In general, the module namespace also exists until the interpreter exits. The statements executed by the top-level invocation of the interpreter.

Whether read from a script file or interactive input, it belongs to a module named _ main _, so it also exists in its own global namespace. The built-in name actually exists in a module, which is called _ builtin __).

The local namespace of a function is created when the function is called and deleted when the function returns or generates an exception that cannot be processed within the function. In fact, if you forget this namespace, it is more suitable for actual use .) Of course, recursive calls have their own local namespace in each recursion.

A scope is a text area in a Python application. A namespace can be directly accessed. "Direct access" refers to finding objects in the namespace directly using a name without any modification. Although the scope is statically defined, the scope is dynamic during use.

At any runtime, there are always exactly three scopes in use, that is, three namespaces are directly accessible): the innermost scope is first searched, contains local names; middle-level scopes, followed by searches, including the Global names of the current module; the outermost scopes are finally searched, including built-in names.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.