[sf-lug] ThinkPython Ex 15.4

Alex Kleider a_kleider at yahoo.com
Sat Feb 28 12:20:17 PST 2009


as a head start on Monday's class:

assume <THIS> is ex15
assume ex15.py is in the working directory and it contains 
your module code (defining classes, procedures, anything else you might want to reuse in other places.)
references to ex15 will refer to the code that is in ex15.py
You can make use of that code either by issuing the statement
from ex15 import *
which 'import's the unqualified (my expression) name space from ex15.ex
import ex15
which provides access to the objects (the name space) in ex15.py but only if you use the preface/qualifier 'ex15.' before each reference.
I.e. if 
class Rectangle ():
is defined in ex15.py
you could make use of it in another file in one of two ways:
from ex15 import *
rect = Rectangle()
import ex15
rect = ex15.Rectangle()
A third scenario would be as follows:
assume that ex15.py also contains a declaration for 
class Circle():
Then you  could 
from ex15 import Circle
which would allow you to reference Circle with out the prefix 
but (I assume) if you wanted to reference Rectangle
?you couldn't do it?
or ?you would have to use 'ex15.Rectangle()'?
I haven't considered this until now and need to experiment to find out.
How am I doing?
It's a name space issue: the convenience of not having to qualify references vs the possibility of have identifier conflicts.
One of the following, has a good section regarding name space as it pertains to Python in general and the use of Classes in particular, I can't remember which:


a_kleider at yahoo.com

--- On Sat, 2/28/09, jim <jim at well.com> wrote:

> From: jim <jim at well.com>
> Subject: Re: [sf-lug] ThinkPython Ex 15.4
> To: alex at kleico.net
> Cc: "Linux userGroup" <sf-lug at linuxmafia.com>
> Date: Saturday, February 28, 2009, 11:00 AM
> monday can we discuss the syntax 
> from <THIS> import <THAT> 
>    per alex' example 
> from World import * 
> On Thu, 2009-02-26 at 11:52 -0800, Alex Kleider wrote:
> > 
> > For those of you who might be following Asheesh's
> Python classes and struggling (as am I) with the excercises:
> > 
> >    The code Allan Downey tells us to use in Chapter
> 15, Ex.4 seems not to work:
> > 
> > from World import *
> > world = World()
> > wait_for_user()
> > 
> > The wait_for_user function is defined in TurtleWorld,
> not in World.
> > 
> > Substituting TurtleWorld  for World in the two places
> that it appears, seems to solve the problem.
> > 
> > 
> > 
> > 
> > 
> > 
> >       
> > 
> > 
> > _______________________________________________
> > sf-lug mailing list
> > sf-lug at linuxmafia.com
> > http://linuxmafia.com/mailman/listinfo/sf-lug
> > 
> _______________________________________________
> sf-lug mailing list
> sf-lug at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/sf-lug


More information about the sf-lug mailing list