importing question ?

Paul Simmonds psimmo60 at hotmail.com
Thu Nov 28 09:50:57 EST 2002


>From: Jonas Geiregat <kemu at sdf-eu.org>
>To: python-list at python.org
>Subject: importing question ?
>Date: Thu, 28 Nov 2002 15:35:47 +0100
>
> >>> import classes
> >>> a = veryBigSnake()
>Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>NameError: name 'veryBigSnake' is not defined
> >>> from classes import *
> >>> a = veryBigSnake()
>
>why doesn't the first import work ?
>and the second does
>
>and what is the difference between those two ?

They both work, it's just that you are looking in the wrong place.
Try:

>>>import classes
>>>a=classes.veryBigSnake()

'from classes import *' brings all the toplevel objects in classes into your 
local namespace, so you can access them just through the name. Generally, 
though, I think this is considered bad practice, as it could create 
overlaps. For instance:

>>>from classes import *
>>>from snakes import *
>>>a=veryBigSnake()

You'll find that a takes its class definition from snakes, not classes, as a 
single name can only be attached to a single object. But:

>>>import classes
>>>import snakes
>>>a=classes.veryBigSnake()
>>>b=snakes.veryBigSnake()

a and b take their class definitions from different modules- no namespace 
overlap- no problem.

HTH,
Paul

>--
>http://mail.python.org/mailman/listinfo/python-list


_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE* 
http://join.msn.com/?page=features/virus





More information about the Python-list mailing list