Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53698 > unrolled thread

Importing Definitions

Started byAzureaus <lo0446@my.bristol.ac.uk>
First post2013-09-05 05:39 -0700
Last post2013-09-06 02:10 -0700
Articles 8 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Importing Definitions Azureaus <lo0446@my.bristol.ac.uk> - 2013-09-05 05:39 -0700
    Re: Importing Definitions Chris Angelico <rosuav@gmail.com> - 2013-09-05 22:50 +1000
      Re: Importing Definitions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-05 17:28 +0000
        Re: Importing Definitions Skip Montanaro <skip@pobox.com> - 2013-09-05 13:06 -0500
        Re: Importing Definitions Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-09-06 11:05 +0100
    Re: Importing Definitions Peter Otten <__peter__@web.de> - 2013-09-05 15:00 +0200
    Re: Importing Definitions Ethan Furman <ethan@stoneleaf.us> - 2013-09-05 05:56 -0700
    Re: Importing Definitions Azureaus <lo0446@my.bristol.ac.uk> - 2013-09-06 02:10 -0700

#53698 — Importing Definitions

FromAzureaus <lo0446@my.bristol.ac.uk>
Date2013-09-05 05:39 -0700
SubjectImporting Definitions
Message-ID<28b859f6-f14b-49d2-9e14-5d3febd33441@googlegroups.com>
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!

[toc] | [next] | [standalone]


#53704

FromChris Angelico <rosuav@gmail.com>
Date2013-09-05 22:50 +1000
Message-ID<mailman.91.1378385806.5461.python-list@python.org>
In reply to#53698
On Thu, Sep 5, 2013 at 10:39 PM, Azureaus <lo0446@my.bristol.ac.uk> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#53729

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-09-05 17:28 +0000
Message-ID<5228bf2d$0$29988$c3e8da3$5496439d@news.astraweb.com>
In reply to#53704
On Thu, 05 Sep 2013 22:50:35 +1000, Chris Angelico wrote:

> On Thu, Sep 5, 2013 at 10:39 PM, Azureaus <lo0446@my.bristol.ac.uk>
> wrote:

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

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!)


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#53733

FromSkip Montanaro <skip@pobox.com>
Date2013-09-05 13:06 -0500
Message-ID<mailman.103.1378404425.5461.python-list@python.org>
In reply to#53729
>> 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.

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

[toc] | [prev] | [next] | [standalone]


#53772

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-09-06 11:05 +0100
Message-ID<mailman.117.1378461963.5461.python-list@python.org>
In reply to#53729
On 5 September 2013 19:06, Skip Montanaro <skip@pobox.com> wrote:
>>> 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.
>
> 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

[toc] | [prev] | [next] | [standalone]


#53705

FromPeter Otten <__peter__@web.de>
Date2013-09-05 15:00 +0200
Message-ID<mailman.92.1378385995.5461.python-list@python.org>
In reply to#53698
Azureaus wrote:

> 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 ;)

[toc] | [prev] | [next] | [standalone]


#53710

FromEthan Furman <ethan@stoneleaf.us>
Date2013-09-05 05:56 -0700
Message-ID<mailman.96.1378388635.5461.python-list@python.org>
In reply to#53698
On 09/05/2013 05:39 AM, Azureaus wrote:
>
> 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

--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#53770

FromAzureaus <lo0446@my.bristol.ac.uk>
Date2013-09-06 02:10 -0700
Message-ID<4a58302f-8b6e-4ba3-92ec-9490b6c98470@googlegroups.com>
In reply to#53698
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!

On Thursday, 5 September 2013 13:39:37 UTC+1, Azureaus  wrote:
> 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!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web