Python imports: Will changing a variable in “child

2019-07-24 01:44发布

Suppose you have 3 modules, a.py, b.py, and c.py:

a.py:

v1 = 1
v2 = 2
etc.

b.py:

from a import *

c.py:

from a import *
v1 = 0

Will c.py change v1 in a.py and b.py? If not, is there a way to do it?

标签: python import
3条回答
淡お忘
2楼-- · 2019-07-24 02:12

All that a statement like:

v1 = 0

can do is bind the name v1 to the object 0. It can't affect a different module.

If I'm using unfamiliar terms there, and I guess I probably am, I strongly recommend you read Fredrik Lundh's excellent article Python Objects: Reset your brain.

查看更多
乱世女痞
3楼-- · 2019-07-24 02:24

The from ... import * form is basically intended for handy interactive use at the interpreter prompt: you'd be well advised to never use it in other situations, as it will give you nothing but problems.

In fact, the in-house style guide at my employer goes further, recommending to always import a module, never contents from within a module (a module from within a package is OK and in fact recommended). As a result, in our codebase, references to imported things are always qualified names (themod.thething) and never barenames (which always refer to builtin, globals of this same module, or locals); this makes the code much clearer and more readable and avoids all kinds of subtle anomalies.

Of course, if a module's name is too long, an as clause in the import, to give it a shorter and handier alias for the purposes of the importing module, is fine. But, with your one-letter module names, that won't be needed;-).

So, if you follow the guideline and always import the module (and not things from inside it), c.v1 will always be referring to the same thing as a.v1 and b.v1, both for getting AND setting: here's one potential subtle anomaly avoided right off the bat!-)

Remember the very last bit of the Zen of Python (do import this at the interpreter prompt to see it all):

Namespaces are one honking great idea -- let's do more of those!

Importing the whole module (not bits and pieces from within it) preserves its integrity as a namespace, as does always referring to things inside the imported module by qualified (dotted) names. It's one honking great idea: do more of that!-)

查看更多
可以哭但决不认输i
4楼-- · 2019-07-24 02:29

Yes, you just need to access it correctly (and don't use import *, it's evil)

c.py:

import a
print a.v1 # prints 1
a.v1 = 0
print a.v1 # prints 0
查看更多
登录 后发表回答