Python Basics Supplement

Source: Internet
Author: User

about how modules are imported:

Import Random

Print Random.choice (range (10))

And

From random import choice

Print Choice (range (10))

The first method is to set the module's name to a global variable that is implicitly in the namespace, so that it accesses the choice function as if it were a global property;

The second method: The choice is introduced directly into the global namespace (not the module name), so it is no longer necessary to put this property in the original module members, in fact, we only have this property;

The misconception in Python is that the second method imports only a function without importing the entire module, which is wrong; the entire module has been imported, but only the reference to that function is saved, all; From-import

This syntax does not bring a difference in performance, and there is no memory savings to say;

Can I import the module repeatedly:

A lot of people worry about a problem is that there are two modules m.py and n.py have imported the foo.py module, when m import N, Foo was not imported two times? Simply put, when Python encounters an already loaded module and is imported,

It skips the loading process, so there's no need to worry about additional memory consumption;

Python is "referenced" or "passed":

This problem can not be simple to use is or not to answer, can only say that the situation depends on-----some objects in the incoming function is a reference, and some are copied in, that is, the value of the judgment is based on the change of the object to see

(mutability), which depends on the type of object, which is often used by Python programmers rather than "pass-through" or "pass-through", instead of a variable (mutable) or immutable (immutable) object.
Simple types or "scalar" types, including integers or other numeric types, string types such as STR and Unicode, and tuples are immutable;

Lists, dictionaries, classes, class instances, and so on are all mutable;

Instance:

1>>> mylist = [1,'a', ['223','Bar']2 ... ]3>>> Mylist2 =list (mylist)4>>> Mylist2[0] = 25>>> Mylist2[2][0] ='zzz' #修改列表的第一个元素6>>>PrintMyList7[1,'a', ['zzz','Bar']]8>>>PrintMylist29[2,'a', ['zzz','Bar']]Ten>>>

Summary: Immutable objects (integers, etc.) are actually copied, and mutable objects simply copy a reference to them, that is, there is only one copy of the object in memory, and there are two references;

This involves a "deep copy" problem.

Python Basics Supplement

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.