Common errors in Python programming

Source: Internet
Author: User
Tags class definition constructor file system reference in python

Unary tuples of single elements:

(1) is not a tuple, in fact, in many overloads of the parentheses, such as in the expression, the function of parentheses is grouped, (1,) This is a single element of the tuple.

Module:

The import module is to set the name of the module to an implied namespace variable.

The From module import method is imported into the current full namespace.

For the second method, not only one function is imported, but the entire module has been imported. But the reference to that function is saved, so from...import this syntax does not make a difference in performance, nor does it save any memory.

Can you repeat the import of a module,

There are two modules a,b are imported into the C module, when a import to B, C is not been imported two times?

Python has import modules and load modules, a module can be imported multiple times, but only to be loaded once, when Python encounters a loaded module and is imported, he will skip the loading process, so there is no need to worry about additional memory consumption problems.

Package:

Package is a way for Python to publish a set of modules on a file system, using common points to access the sub modules, each with a __init__.py file, This tells the Python interpreter that the files in these directories should be treated as a child package rather than as a normal file. Under normal circumstances are empty files, you can do some initialization work, for points to access the way you can use the alias to play a few letters.

To be modified:

A "quote" or "pass value" is usually not applicable to Python, and is replaced by a variable or immutable object. Change refers to whether the value of an object changes. Integers and strings and tuples are immutable, and lists, dictionaries, classes, class instances, and so on are mutable.

Effect on method invocation: If the called function has any mutable objects, it is usually directly modified to modify its data structure directly instead of returning a copy of the modified object.

To copy objects and variability:

Immutable objects are copied, and mutable objects are copied by a reference to them.

Replication is divided into shallow and deep copies, and shallow copies only copy a reference to a Mutable object.

Constructor vs Initialization Program:

One of the differences between Python and the traditional OOP language is that it doesn't have an explicit constructor concept, and there is no new keyword in Python because we don't really instantiate your class. Python creates the instance and invokes the initialization program. __init__ method.

The properties of an instance can be dynamically assigned, even after the class definition has been completed or even created, and the properties can be dynamically created at run time.

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.