Python newbie needs help

Dennis Benzinger Dennis.Benzinger at gmx.net
Wed Dec 14 20:40:51 CET 2005


Ron Hudson schrieb:
> [...]
> I have a .py file with some def scripts(?) in it. 

def is the begining of a function definition.

> [...]
> I am using "Import" to read it after I start an interactive python.  
> What I need
> right now is I seem to have some sort of scoping problems with the  
> world dictionary.
> 
> I can read it and write it and "world<cr>" lists it out. but my def  
> look(at): script
> it seems it doesn't see the the "world" dictionary.
> 
>  >>> import lets
>  >>> world = lets.loadworld()
>  >>> world
> {'wizarddescription':'A short guy wearing a robe and a pointy hat'}
>  >>> lets.look('wizard')
>      file "<stdin>" line ?
>      file lets.py line 14 in look
>         print world[at+'description']
> nameError:world
>  >>> at = 'wizard'
>  >>> print world[at+'description']
> A short guy wearing a robe and a pointy hat
>  >>>
> 
> Is there a way to make 'world' global? can I do it in lets.py?
> 
> Am I going about this all wrong?
> [...]

The easiest solution for you would be a world variable in your lets 
module. Then at the interactive prompt you could refer to it with 
lets.world. So the loadworld function in lets.py would look like this:

def loadworld():
     # Your loading code
     global world
     world = ...


Bye,
Dennis



More information about the Python-list mailing list