I'm new to Python and programming in general (a couple of weeks at most).
Concerning Python and using modules, I realise that functions can imported using from a import *
.
So instead of typing
a.sayHi()
a.sayBye()
I can say
sayHi()
sayBye()
which I find simplifies things a great deal. Now, say I have a bunch of variables that I want to use across modules and I have them all defined in one python module. How can I, using a similar method as mentioned above or an equally simple one, import these variables. I don't want to use import a
and then be required to prefix all my variables with a.
.
The following situation would by ideal:
a.py
name = "Michael"
age = 15
b.py
some_function
if name == "Michael":
if age == 15:
print("Simple!")
Output:
Simple!
You didn't say this directly, but I'm assuming you're having trouble with manipulating these global variables.
If you manipulate global variables from inside a function, you must declare them global
If you don't do that, then
a = 15
will just create a local variable and assign it 15, while the global a stays 10You gave the solution yourself:
from a import *
will work just fine. Python does not differentiate between functions and variables in this respect.Just for some context, most linters will flag
from module import *
with a warning, because it's prone to namespace collisions that will cause headaches down the road.Nobody has noted yet that, as an alternative, you can use the
form and then use
name
andage
directly (without thea.
prefix). Thefrom [module] import [identifiers]
form is more future proof because you can easily see when one import will be overriding another.Also note that "variables" aren't different from functions in Python in terms of how they're addressed -- every identifier like
name
orsayBye
is pointing at some kind of object. The identifiername
is pointing at a string object,sayBye
is pointing at a function object, andage
is pointing at an integer object. When you tell Python:you're saying "take those objects pointed at by
name
andage
within modulea
and point at them in the current scope with the same identifiers".Similarly, if you want to point at them with different identifiers on import, you can use the
form. The same function object gets pointed at, except in the current scope the identifier pointing at it is
bidFarewell
whereas in modulea
the identifier pointing at it issayBye
.Like others have said,
will also import the modules variables.
However, you need to understand that you are not importing variables, just references to objects. Assigning something else to the imported names in the importing module won't affect the other modules.
Example: assume you have a module
module.py
containing the following code:Then you have two other modules,
mod1.py
andmod2.py
which both do the following:In each module, two names,
a
andb
are created, pointing to the objects1
and2
, respectively.Now, if somewhere in
mod1.py
you assign something else to the global namea
:the name
a
inmodule.py
and the namea
inmod2.py
will still point to the object1
.So
from module import *
will work if you want read-only globals, but it won't work if you want read-write globals. If the latter, you're better off just importingimport module
and then either getting the value (module.a
) or setting the value (module.a= …
) prefixed by the module.