Importing Definitions

A

Azureaus

Hi all,
Thank you all for your help so far in the group.

Lets say I have some definitions in a module1.py e.g.

import sys
A,B,C,D,E = range(5)
def method1():
more code
end

Then a second module module2.py where I wish to use these definitions
import module1.py
print A

This will throw an error saying "global name 'A' is not defined."

Now I know if there was a method I wanted to reference I could do something like this in module2.
from module1 import method1

which would mean from that point on I could just reference it as method1 rather than module1.method1, is there such a way I could do this with definitions??

Thanks!
 
C

Chris Angelico

Lets say I have some definitions in a module1.py e.g.

import sys
A,B,C,D,E = range(5)
def method1():
more code
end

Then a second module module2.py where I wish to use these definitions
import module1.py
print A

This will throw an error saying "global name 'A' is not defined."

Now I know if there was a method I wanted to reference I could do something like this in module2.
from module1 import method1

which would mean from that point on I could just reference it as method1 rather than module1.method1, is there such a way I could do this with definitions??

You can! Any name will work, functions aren't special.

from module1 import method1, A, B, C, D, E

If you want all of them at once, you may want:

from module1 import *

but this can make your code confusing.

ChrisA
 
E

Ethan Furman

This will throw an error saying "global name 'A' is not defined."

In Python, "global" really means "module-level".

Now I know if there was a method I wanted to reference I could do something like this in module2.
from module1 import method1

which would mean from that point on I could just reference it as method1 rather than module1.method1, is there such a way I could do this with definitions??

from module1 import data1
 
P

Peter Otten

Azureaus said:
This will throw an error saying "global name 'A' is not defined."

Now I know if there was a method I wanted to reference I could do
something like this in module2.
from module1 import method1

What a crazy idea! How could anyone ever think of that!
which would mean from that point on I could just reference it as method1
rather than module1.method1, is there such a way I could do this with
definitions??

Please read the tutorial, especially

http://docs.python.org/2/tutorial/modules.html#more-on-modules

before you come up with more crazy shit like this ;)
 
S

Steven D'Aprano

You can! Any name will work, functions aren't special.

from module1 import method1, A, B, C, D, E

Better practice is to use:

import module1
print module1.A
print module2.B

and so forth since that makes it far more clear what you are doing and
where they come from. But it's not compulsory.


If you want all of them at once, you may want:

from module1 import *

but this can make your code confusing.

Shame on you Chris :) Don't teach newbies to set their head on fire
before teaching them where the fire extinguisher is.

Azureaus, don't use the form "from module import *" unless you really
need to, and as a beginner, you almost never really need to.

(And as an expert, you will really need to even less!)
 
S

Skip Montanaro

You can! Any name will work, functions aren't special.
Better practice is to use:

import module1
print module1.A
print module2.B

and so forth since that makes it far more clear what you are doing and
where they come from. But it's not compulsory.

Maybe I'm imagining things, but I think it's pretty common to see some
big-ass modules imported using "from module import *". While people
may think that's expedient, I think it can often be the source of
subtle bugs.

Skip
 
A

Azureaus

Thanks for the advice, much appreciated - I didn't realise you could also import definitions. I do always read the documentation before posting but sometimes I don't know how it's necessarily applicable to my own case sometimes - hence the post. I'll avoid using '*' at all costs, I've had the pleasure of going through lots of Python code recently not written by myself and I can see how that would make it a total nightmare to figure out what was going on.

I think Python is awesome and look forward to actually getting good with it..
Cheers!
 
O

Oscar Benjamin

Maybe I'm imagining things, but I think it's pretty common to see some
big-ass modules imported using "from module import *". While people
may think that's expedient, I think it can often be the source of
subtle bugs.

It is common in scientific programming e.g. 'from numpy import *' and
so on. And actually I think that it is a reasonable thing to do in
cases where:
1) You're writing a single smallish module/script.
2) The imported module is well-known and anyone who doesn't know it
won't understand your code anyway.
3) You're only doing one star-import.
4) You're using a lot of symbols from that import in lots of places in
your code.

Consider for example, this script:

#!/usr/bin/env python

import numpy
import matplotlib.pyplot

k = 1
f = 10
x = numpy.arange(0, 3, 0.01)
y = numpy.exp(k * x) * numpy.sin(2 * numpy.pi * x)

matplotlib.pyplot.plot(x, y)
matplotlib.pyplot.show()

I write a lot of scripts that look roughly like that except they're
usually quite a bit bigger. Written as above, the word numpy would
appear dozens of times in a single script. Because of this it's
commonplace to abbreviate the module names on import e.g.:

#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt

k = 1
f = 10
x = np.arange(0, 3, 0.01)
y = np.exp(k * x) * np.sin(2 * np.pi * x)

plt.plot(x, y)
plt.show()

But still in situations where you have to write 'np.' 3 or more times
on a line, it just becomes a distraction in your code. Of course you
can just import the names you want e.g.:

#!/usr/bin/env python

from numpy import arange, exp, sin, pi
from matplotlib.pyplot import plot, show

k = 1
f = 10
x = arange(0, 3, 0.01)
y = exp(k * x) * sin(2 * pi * x)

plot(x, y)
show()

But this just gets annoying. In practice I'll probably end up
importing 20 or so names. When writing a script like this I probably
tweak and rerun it continuously and so when I decide that actually I
meant cos not sin then I run it and get:

$ ./tmp3.py
Traceback (most recent call last):
File "./tmp3.py", line 9, in <module>
y = exp(k * x) * cos(2 * pi * x)
NameError: name 'cos' is not defined

And then I have to go back and edit the import line. This kind of
thing just wastes a bit of time while I'm working. So the matplotlib
folks created a special module called "pylab" that brings together the
symbols from numpy and matplotlib and is specifically designed so that
you can star-import it when writing this kind of code interactively or
in a script:

#!/usr/bin/env python

from pylab import *

k = 1
f = 10
x = arange(0, 3, 0.01)
y = exp(k * x) * sin(2 * pi * x)

plot(x, y)
show()

The pylab star-import brings a *huge* number of symbols into the
current namespace:

$ python -c 'import pylab; print(len(pylab.__dict__))'
933

However, it's very common that this set of symbols comprises
everything that a script needs. Personally I don't think there's
anything wrong with using that in a script (with the caveats I
mentioned above). A similar case is in a script that uses sympy (I
wouldn't do it with pylab and sympy together though). In either case I
would tend to think that I'm essentially using an augmentation of
builtins. Just as you need to know roughly what's in builtins to
understand ordinary Python code, you need to know what's in pylab or
sympy to understand this script.


Oscar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top