A pit in Python from module import *

Source: Internet
Author: User
But there is another problem-you think you have modified a variable, in fact, the From module import * After the that is not updated, very dangerous, because the program may also be able to run normally, but the result is wrong, to the production was found to be more miserable.

As an example:

You have defined some variables in the base module:

# Reference Data Typeclass demo:def __init__ (self, name):  self.name = Namedemo = Demo (' demo ') # primitive Typefoo = 1

Then read it in a module using the From module import:

From base import *def read ():    print ' Reference Data ID: ' + str (demo)    ' print ' Reference data value: ' + DEMO.N Ame    print ' Primitive data ID: ' + str (ID (foo))    print ' Primitive data value: ' + str (foo)

Write it in a different module:

Import Basedef write (): print "\noriginal:" print "Original reference Data ID:" + str (ID (base.demo)) Base.demo.name = "Up Dated demo "# This would reflect the change #base. Demo = base. Demo ("Updated demo") # won ' t relfect the change print "Original Data ID:" + str (ID (base.foo)) Base.foo = Print "Original Data ID after assignment:" + str (ID (base.foo))

Then write first, read later, see if the content is valid:

Import Readimport Writeprint "before write" Read.read () write.write () print "\nafter write" Read.read ()

The conclusion is no, the reason is:

When you use the From module import, actually copy a copy of reference or pointer, point to a memory, VAR and module.var all point to the same part of memory
When you modify Module.var, you actually point it to another memory, where Var and module.var point to different memory
So, although the value of Module.var has changed, Var still points to the original memory, the original value
This is easy to understand for object, you can directly modify the value in the object, this is valid, but when you point to another object, it is invalid. For the primitive type, it is actually a reason, because each assignment is to point to a different memory address, rather than inplace modify the existing memory-this is easy to verify:

In [1]: a = 10In [2]: ID (a) out[2]: 20429204In [3]: a = 100In [4]: ID (a) out[4]: 20430108

Therefore, it is recommended that you do not use the From module import * Unless it is a quick and dirty script!

Example: Https://github.com/baiyanhuang/blog/tree/master/arena/python/from_module_import

  • 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.