How can i use a variable without define it ?

Cristina Yenyxe González García the.blue.valkyrie at gmail.com
Wed Jul 16 06:39:53 EDT 2008


Hello.

2008/7/16 zhw <weizhonghua.ati at gmail.com>:
> On 7月16日, 下午5时35分, Ben Finney <bignose+hates-s... at benfinney.id.au>
> wrote:
>> zhw <weizhonghua.... at gmail.com> writes:
>> > Here is a example that I want to complete:
>> > >>> import sys, new
>> > >>> context={"name":"david", "sex":"male"}
>>
>> Here you have a set of values addressible by name.
>>
>> > >>> sys.modules["foo"] = new.module("foo")
>>
>> Why do you believe you need to create a module object?
>>
>> > >>> import foo
>> > >>> for attr in context:
>> >    setattr(foo, attr, context[attr])
>>

Forgetting these considerations (I agree with Ben, though), your error
is trying to use an 'import *' inside a function. You should take it
outside the function, and try to run it again.

>>
>> > >>> def bar():
>> >         # here is a error
>> >         # import * only allowed at module level
>> >    from foo import *
>> >         print name, sex
>>

There are different import statements you can use in Python. I
recommend you to take a look at the tutorial to learn about the
differences between them: http://docs.python.org/tut/node8.html

Good luck.


More information about the Python-list mailing list