How can i use a variable without define it ?

Discussion in 'Python' started by zhw, Jul 16, 2008.

  1. zhw

    zhw Guest

    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"。
     
    zhw, Jul 16, 2008
    #1
    1. Advertising

  2. zhw

    zhw Guest

    On 716, 447, Ben Finney <>
    wrote:
    > zhw <> 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()
     
    zhw, Jul 16, 2008
    #2
    1. Advertising

  3. zhw

    zhw Guest

    On 716, 535, Ben Finney <>
    wrote:
    > zhw <> 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!
     
    zhw, Jul 16, 2008
    #3
  4. zhw

    Chris Guest

    On Jul 16, 11:06 am, zhw <> wrote:
    > On 716, 447, Ben Finney <>
    > wrote:
    >
    >
    >
    > > zhw <> 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()

    >
    >


    def bar():
    from foo import name, sex
    print name, sex

    You will need to know beforehand what attributes you want to import
    into the function.
     
    Chris, Jul 16, 2008
    #4
  5. zhw

    Guest

    On 16 juil, 11:06, zhw <> wrote:
    > On 716, 447, Ben Finney <>
    > wrote:
    >
    >
    >
    > > zhw <> 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()
     
    , Jul 17, 2008
    #5
  6. zhw

    Tim Roberts Guest

    zhw <> wrote:
    >Ben Finney <> >wrote:
    >>
    >> What problem are you trying to solve?

    >
    >I an sorry, I can't tell you.


    That's nonsense. No one is asking you to reveal the design of your
    company's next product. However, you have some overall problem you are
    trying to solve, and you have focused in on one POSSIBLE solution. Instead,
    tell us about the PROBLEM, and we'll offer good solutions.

    >If you can't give a solution, just ignore it!


    NO ONE will be able to give you a solution, because you have not described
    the problem.
    --
    Tim Roberts,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Jul 18, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. theotyflos
    Replies:
    3
    Views:
    482
    Thomas Matthews
    Feb 19, 2004
  2. Peng Yu

    When to use #define and When to use const?

    Peng Yu, Dec 7, 2004, in forum: C Programming
    Replies:
    7
    Views:
    359
    CBFalconer
    Dec 8, 2004
  3. robin liu
    Replies:
    3
    Views:
    832
    Robin Liu
    Apr 21, 2006
  4. Replies:
    4
    Views:
    422
    Alf P. Steinbach
    May 5, 2006
  5. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    482
    Brian Takita
    Jan 23, 2006
Loading...

Share This Page