How can i use a variable without define it ?

Z

zhw

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"$B!#(B
 
Z

zhw

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])
# here is a error
# import * only allowed at module level
from foo import *
print name, sex
 
Z

zhw

zhw said:
Here is a example that I want to complete:

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.
# here is a error
# import * only allowed at module level
from foo import *
print name, sex

You can simply do:
... print context['name'], context['sex']
...david male

Or, more flexible and more explicit:
... print context['name'], context['sex']
...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!
 
C

Chris

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])

# here is a error
# import * only allowed at module level
from foo import *
print name, sex

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.
 
B

bruno.desthuilliers

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])

# here is a error
# import * only allowed at module level
from foo import *
print name, sex

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()
 
T

Tim Roberts

zhw said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top