Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How can i use a variable without define it ?

Reply
Thread Tools

How can i use a variable without define it ?

 
 
zhw
Guest
Posts: n/a
 
      07-16-2008
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"。

 
Reply With Quote
 
 
 
 
zhw
Guest
Posts: n/a
 
      07-16-2008
On 716, 447, Ben Finney <(E-Mail Removed)>
wrote:
> zhw <(E-Mail Removed)> 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()



 
Reply With Quote
 
 
 
 
zhw
Guest
Posts: n/a
 
      07-16-2008
On 716, 535, Ben Finney <(E-Mail Removed)>
wrote:
> zhw <(E-Mail Removed)> 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!

 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      07-16-2008
On Jul 16, 11:06 am, zhw <(E-Mail Removed)> wrote:
> On 716, 447, Ben Finney <(E-Mail Removed)>
> wrote:
>
>
>
> > zhw <(E-Mail Removed)> 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.
 
Reply With Quote
 
bruno.desthuilliers@gmail.com
Guest
Posts: n/a
 
      07-17-2008
On 16 juil, 11:06, zhw <(E-Mail Removed)> wrote:
> On 716, 447, Ben Finney <(E-Mail Removed)>
> wrote:
>
>
>
> > zhw <(E-Mail Removed)> 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()
 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      07-18-2008
zhw <(E-Mail Removed)> wrote:
>Ben Finney <(E-Mail Removed)> >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, http://www.velocityreviews.com/forums/(E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
use a const variable as opposed to #define - Multiple const objects created yancheng.cheok@gmail.com C++ 4 05-05-2006 10:30 AM
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
#define _ and #define __ Brian Takita Ruby 0 01-23-2006 04:34 AM
How to define a define that defines some defines ? theotyflos C Programming 3 02-19-2004 05:07 PM



Advertisments