How can i use a variable without define it ?

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Thu Jul 17 08:13:23 EDT 2008


On 16 juil, 11:06, zhw <weizhonghua.... at gmail.com> wrote:
> On 7月16日, 下午4时47分, Ben Finney <bignose+hates-s... at benfinney.id.au>
> wrote:
>
>
>
> > zhw <weizhonghua.... at gmail.com> 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()

Looks like a major WTF to me. What's wrong with:

class Person(object):
    def __init__(self, name, sex):
        self.name = name
        self.sex = sex
    def bar(self):
        print self.name, self.sex

p = Person("david", "male")
p.bar()



More information about the Python-list mailing list