How can i use a variable without define it ?
How can i use a variable without define it ? I have thought about the __import__ function, but the docs says "the __import__() function does not set the local variable named eggs"。 -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i use a variable without define it ?
On 7月16日, 下午4时47分, Ben Finney <[EMAIL PROTECTED]> wrote: > zhw <[EMAIL PROTECTED]> writes: > > How can i use a variable without define it ? > > What do you mean by "use"? That's so vague I can think of many > possible interpretations. > > What do you mean by "variable"? That term carries a lot of baggage > that doesn't apply in Python. > > Can you give a small, complete example that demonstrates the issue > you're trying to solve? > > -- > \ “The face of a child can say it all, especially the mouth part | > `\of the face.” ―Jack Handey | > _o__) | > Ben Finney Thank you! Sorry for my poor english! Here is a example that I want to complete: >>> import sys, new >>> context={"name":"david", "sex":"male"} >>> sys.modules["foo"] = new.module("foo") >>> import foo >>> for attr in context: setattr(foo, attr, context[attr]) >>> def bar(): # here is a error # import * only allowed at module level from foo import * print name, sex >>> bar() -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i use a variable without define it ?
On 7月16日, 下午5时35分, Ben Finney <[EMAIL PROTECTED]> wrote: > zhw <[EMAIL PROTECTED]> 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]) > > This doesn't appear to get you anything that isn't already available > with the 'context' mapping. > > > >>> def bar(): > > # here is a error > > # import * only allowed at module level > >from foo import * > > print name, sex > > You can simply do: > > >>> context = {'name': "david", 'sex': "male"} > >>> def bar(): > ... print context['name'], context['sex'] > ... > >>> bar() > david male > > Or, more flexible and more explicit: > > >>> foo = {'name': "david", 'sex': "male"} > >>> def bar(context): > ... print context['name'], context['sex'] > ... > >>> bar(foo) > david male > > What problem are you trying to solve? I an sorry, I can't tell you. If you can't give a solution, just ignore it! -- http://mail.python.org/mailman/listinfo/python-list