Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

variable hell

4 views
Skip to first unread message

Nx

unread,
Aug 25, 2005, 9:19:10 AM8/25/05
to

Hi

I am unpacking a list into variables, for some reason they need to be
unpacked into variable names like a0,a1,a2....upto aN whatever is
in the list.

How to create the variables dynamically ?

I am looking for something like
pseudo code line follows :

a%s = str(value)


here below is a snippet from the mylist unpack code

#next lines cut the end of line character from each line in the list
mylist = [line[:-1] for line in mylist]

for index,value in enumerate(mylist):
if index == 0 :
a0 = str(value)
print "a0 : ",a0
elif index == 1 :
a1 = str(value)
print "a1 : ",a1

Thanks
Nx

Diez B. Roggisch

unread,
Aug 25, 2005, 9:34:26 AM8/25/05
to

Nx wrote:
> I am unpacking a list into variables, for some reason they need to be
> unpacked into variable names like a0,a1,a2....upto aN whatever is
> in the list.

Explain this "some reason". This smells, and the way to go would be to
use a dict mapping a_n to whatever is in the list - not creating
variables. How do you want to access generated variables anyway -
especially when you don't have the faintest idea how many of them there
are? Obviously there can't be code written based on that.

Regards,

Diez

Sybren Stuvel

unread,
Aug 25, 2005, 9:41:51 AM8/25/05
to
Nx enlightened us with:

> I am unpacking a list into variables, for some reason they need to
> be unpacked into variable names like a0,a1,a2....upto aN whatever is
> in the list.

You're probably doing things the wrong way. What is your ultimate goal
with this? There is probably a better way of doing it.

In the mean time, look at eval().

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa

Robert Kern

unread,
Aug 25, 2005, 9:35:04 AM8/25/05
to pytho...@python.org
Nx wrote:
> Hi
>
> I am unpacking a list into variables, for some reason they need to be
> unpacked into variable names like a0,a1,a2....upto aN whatever is
> in the list.

Really? Why?

--
Robert Kern
rk...@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Fredrik Lundh

unread,
Aug 25, 2005, 9:41:06 AM8/25/05
to pytho...@python.org
"Nx" <nom...@nomail.com> wrote:

> I am unpacking a list into variables, for some reason they need to be
> unpacked into variable names like a0,a1,a2....upto aN whatever is
> in the list.

why?

</F>

Peter Maas

unread,
Aug 25, 2005, 9:47:37 AM8/25/05
to
Nx schrieb:

> Hi
>
> I am unpacking a list into variables, for some reason they need to be
> unpacked into variable names like a0,a1,a2....upto aN whatever is
> in the list.
>
> How to create the variables dynamically ?
>
> I am looking for something like
> pseudo code line follows :
>
> a%s = str(value)

>>> suffix = 'var'
>>> vars()['a%s' % suffix] = 45
>>> avar
45

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------

Benji York

unread,
Aug 25, 2005, 10:20:28 AM8/25/05
to pytho...@python.org
Peter Maas wrote:
> >>> suffix = 'var'
> >>> vars()['a%s' % suffix] = 45
> >>> avar
> 45

Quoting from http://docs.python.org/lib/built-in-funcs.html#l2h-76 about
the "vars" built in:

The returned dictionary should not be modified: the effects on the
corresponding symbol table are undefined.
--
Benji York

Nx

unread,
Aug 25, 2005, 10:43:04 AM8/25/05
to
Thanks for the many replies

here is an example for what it will be used for , in this case
fixed at 31 fieldvalues:

inputvalues=(s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16,s17,s18,s19,s20,s21,s22,s23,s24,s25,
s26,s27,s28,s29,s30,s31)
MYINSERTSELECT = "INSERT INTO
ADDRESS(ALIAS,COMPANY,ADDRESSLI1,ADDRESSLI2,ADDRESSCO,TOWN,ZIP,COUNTRY,TEL1,TEL2,FAX,EMAIL,INTERNET,PERSON1,TITLE1,RES1,PERSON2,TITLE2,RES2,PERSON3,TITLE3,RES3,PERSON4,TITLE4,RES4,PERSON5,TITLE5,RES5,PRODALIAS,PAGER,TLX,ADDMEMO)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

con1.commit()
cur = con1.cursor()
try :
cur.execute(MYINSERTSELECT,inputvalues)
con1.commit()
print 'Inserted 1 record'
except IOError, (errno, strerror):
print "I/O error(%s): %s" % (errno, strerror)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unexpected error:", sys.exc_info()[0]
raise


I am sure there is an easier way, but I have not found it yet.

Nx

bruno modulix

unread,
Aug 25, 2005, 10:59:26 AM8/25/05
to
Nx wrote:
> Thanks for the many replies
>
> here is an example for what it will be used for , in this case
> fixed at 31 fieldvalues:
>
> inputvalues=(s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16,s17,s18,s19,s20,s21,s22,s23,s24,s25,
> s26,s27,s28,s29,s30,s31)
> MYINSERTSELECT = "INSERT INTO
> ADDRESS(ALIAS,COMPANY,ADDRESSLI1,ADDRESSLI2,ADDRESSCO,TOWN,ZIP,COUNTRY,TEL1,TEL2,FAX,EMAIL,INTERNET,PERSON1,TITLE1,RES1,PERSON2,TITLE2,RES2,PERSON3,TITLE3,RES3,PERSON4,TITLE4,RES4,PERSON5,TITLE5,RES5,PRODALIAS,PAGER,TLX,ADDMEMO)
> VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
>
> con1.commit()
> cur = con1.cursor()
> try :
> cur.execute(MYINSERTSELECT,inputvalues)

If I refer to your original post, there's someting I dont understand:


"""
I am unpacking a list into variables, for some reason they need to be
unpacked into variable names like a0,a1,a2....upto aN whatever is
in the list.
"""

Why unpack inputvalues if your next step is to pack'em back again ? Or
what did I miss ?


--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'on...@xiludom.gro'.split('@')])"

Carsten Haese

unread,
Aug 25, 2005, 11:04:54 AM8/25/05
to pytho...@python.org
On Thu, 2005-08-25 at 10:43, Nx wrote:
> Thanks for the many replies
>
> here is an example for what it will be used for , in this case
> fixed at 31 fieldvalues:
>
> inputvalues=(s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16,s17,s18,s19,s20,s21,s22,s23,s24,s25,
> s26,s27,s28,s29,s30,s31)

inputvalues = tuple(mylist)

Hope this helps,

Carsten.


Adriaan Renting

unread,
Aug 25, 2005, 11:06:54 AM8/25/05
to pytho...@python.org
You might be able to do something along the lines of

for count in range(0,maxcount):
value = values[count]
exec(eval("'a%s=%s' % (count, value)"))

But I am also wonder: why?

Carsten Haese

unread,
Aug 25, 2005, 11:11:00 AM8/25/05
to pytho...@python.org
On Thu, 2005-08-25 at 11:04, I hastily wrote:
> On Thu, 2005-08-25 at 10:43, Nx wrote:
> > Thanks for the many replies
> >
> > here is an example for what it will be used for , in this case
> > fixed at 31 fieldvalues:
> >
> > inputvalues=(s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16,s17,s18,s19,s20,s21,s22,s23,s24,s25,
> > s26,s27,s28,s29,s30,s31)
>
> inputvalues = tuple(mylist)

And actually, you probably don't have to do that, because the execute
method should be able to handle a list just as well as a tuple.

-Carsten.


Nx

unread,
Aug 25, 2005, 11:17:43 AM8/25/05
to

> """
>
> Why unpack inputvalues if your next step is to pack'em back again ? Or
> what did I miss ?
>
The original values in this case are being read from a text file
with one value including a linefeed per line and the original idea was,
that having them read into a list was the best way to massage them into the
form required to be used as input values for the insert statement.

Nx

sp1...@gmail.com

unread,
Aug 25, 2005, 11:32:07 AM8/25/05
to
Hey, if the man wants to write it that way, let the man write it that
way. If it works for him, great... he's sure confused the heck out of
all of us, and that translates into job security for him! As you can
see, the name of the post is 'variable hell' and that is exactly what
he is creating, so "Adriaan Renting", excellent response!

Robert Kern

unread,
Aug 25, 2005, 11:31:51 AM8/25/05
to pytho...@python.org

Again, why unpack them into separate variables when they are *already*
in the form that you want to use them?

rafi

unread,
Aug 25, 2005, 12:03:27 PM8/25/05
to
Adriaan Renting wrote:
> You might be able to do something along the lines of
>
> for count in range(0,maxcount):
> value = values[count]
> exec(eval("'a%s=%s' % (count, value)"))

why using the eval?

exec ('a%s=%s' % (count, value))

should be fine

--
rafi

"Imagination is more important than knowledge."
(Albert Einstein)

Ron Garret

unread,
Aug 25, 2005, 12:08:14 PM8/25/05
to
In article <mailman.3508.1124979...@python.org>,
Benji York <be...@benjiyork.com> wrote:

If you really want to make something like this work you can define a
class that would work like this:

vars = funkyclass()
varname = 'x'
vars[varname] = value
vars.x

But this is clearly a design mistake. Either you know the names of the
variables when you write the code or you do not. If you know them you
can simply assign them directly. If you do not know them then you can't
put them in the code to read their values anyway, and what you need is
just a regular dictionary.

rg

Robert Kern

unread,
Aug 25, 2005, 12:22:15 PM8/25/05
to pytho...@python.org
Ron Garret wrote:

> If you really want to make something like this work you can define a
> class that would work like this:
>
> vars = funkyclass()
> varname = 'x'
> vars[varname] = value
> vars.x
>
> But this is clearly a design mistake. Either you know the names of the
> variables when you write the code or you do not. If you know them you
> can simply assign them directly. If you do not know them then you can't
> put them in the code to read their values anyway, and what you need is
> just a regular dictionary.

In fact, I do this all of the time.

class Bunch(dict):
def __init__(self, *args, **kwds):
dict.__init__(self, *args, **kwds)
self.__dict__ = self

It's a lifesaver when you're working at the interactive prompt. In the
bowels of my modules, I may not know what the contents are at code-time,
but at the prompt I probably do. Bunch assists both usages.

Steve Holden

unread,
Aug 25, 2005, 12:48:37 PM8/25/05
to pytho...@python.org
Carsten Haese wrote:
> On Thu, 2005-08-25 at 11:04, I hastily wrote:
>
>>On Thu, 2005-08-25 at 10:43, Nx wrote:
>>
>>>Thanks for the many replies
>>>
>>> here is an example for what it will be used for , in this case
>>> fixed at 31 fieldvalues:
>>>
>>> inputvalues=(s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15,s16,s17,s18,s19,s20,s21,s22,s23,s24,s25,
>>> s26,s27,s28,s29,s30,s31)
>>
>>inputvalues = tuple(mylist)
>
>
> And actually, you probably don't have to do that, because the execute
> method should be able to handle a list just as well as a tuple.
>
That depends on the database module. Some will insist in tuples, IIRC.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

bruno modulix

unread,
Aug 25, 2005, 2:54:15 PM8/25/05
to

Ron Garret

unread,
Aug 25, 2005, 3:28:30 PM8/25/05
to
In article <mailman.3516.1124987...@python.org>,
Robert Kern <rk...@ucsd.edu> wrote:

> In the
> bowels of my modules, I may not know what the contents are at code-time,

Then how do you write your code?

rg

Reinhold Birkenfeld

unread,
Aug 25, 2005, 3:45:01 PM8/25/05
to
rafi wrote:
> Adriaan Renting wrote:
>> You might be able to do something along the lines of
>>
>> for count in range(0,maxcount):
>> value = values[count]
>> exec(eval("'a%s=%s' % (count, value)"))
>
> why using the eval?
>
> exec ('a%s=%s' % (count, value))
>
> should be fine

And this demonstrates why exec as a statement was a mistake ;)

It actually is

exec 'a%s=%s' % (count, value)

Reinhold

rafi

unread,
Aug 25, 2005, 4:15:11 PM8/25/05
to
Reinhold Birkenfeld wrote:

>>> exec(eval("'a%s=%s' % (count, value)"))
>>
>>why using the eval?
>>
>>exec ('a%s=%s' % (count, value))
>>
>>should be fine
>
> And this demonstrates why exec as a statement was a mistake ;)
>
> It actually is
>
> exec 'a%s=%s' % (count, value)

Noted.

In the meantime another question I cannot find an answer to: any idea
why does eval() consider '=' as a syntax error?

>>> eval ('a=1')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 1
a=1
^
SyntaxError: invalid syntax

Thanks

Robert Kern

unread,
Aug 25, 2005, 4:17:03 PM8/25/05
to pytho...@python.org

With style. ;-)

I use a Bunch where I might otherwise use a dictionary inside my modules
because it *is* a dictionary. Interactively, I'll usually use it as an
object with attributes. The keys are usually ideosyncratic, like station
codes for permanent GPS stations (e.g. "CAND", "USLO", "MNMC", "MIDA"),
rather than generic (e.g. "northing", "day").

So I might have a function that do some analysis on all of the GPS
stations within a Bunch. I don't know the names of the stations when I'm
writing the function, but I can iterate over the keys and values in the
Bunch.

def subtract_reference(data, refstation):
"""Subtract the motion of the reference station from the remaining
timeseries.
"""
refdata = data[refstation]
for station in data:
if station == refstation:
continue
data[station].northing -= refdata.northing
data[station].easting -= refdata.easting
# ...

At the prompt, though, I may want to plot CAND's timeseries.

In [10]: plot(data.CAND.t, data.CAND.northing)

Bunch allows me to use "data[station]" and "data.CAND" as the situation
demands rather than forcing me to use the clunkier "getattr(data,
station)" or "data['CAND']", respectively.

Robert Kern

unread,
Aug 25, 2005, 4:23:02 PM8/25/05
to pytho...@python.org
rafi wrote:

> In the meantime another question I cannot find an answer to: any idea
> why does eval() consider '=' as a syntax error?
>
> >>> eval ('a=1')
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "<string>", line 1
> a=1
> ^
> SyntaxError: invalid syntax

eval *evaluates* an expression. "a=1" is a statement. It has no value.

Steve Holden

unread,
Aug 25, 2005, 4:29:11 PM8/25/05
to pytho...@python.org
rafi wrote:
> Reinhold Birkenfeld wrote:
>
>
>>>> exec(eval("'a%s=%s' % (count, value)"))
>>>
>>>why using the eval?
>>>
>>>exec ('a%s=%s' % (count, value))
>>>
>>>should be fine
>>
>>And this demonstrates why exec as a statement was a mistake ;)
>>
>>It actually is
>>
>>exec 'a%s=%s' % (count, value)
>
>
> Noted.
>
> In the meantime another question I cannot find an answer to: any idea
> why does eval() consider '=' as a syntax error?
>
> >>> eval ('a=1')
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "<string>", line 1
> a=1
> ^
> SyntaxError: invalid syntax
>
> Thanks
>
Because eval() takes an expression as an argument, and assignment is a
statement.

rafi

unread,
Aug 25, 2005, 4:41:51 PM8/25/05
to
Steve Holden wrote:

> Because eval() takes an expression as an argument, and assignment is a
> statement.

I am definitely not a language lawyer... but I should a little bit more

thanks,

Ron Garret

unread,
Aug 25, 2005, 7:25:38 PM8/25/05
to
In article <mailman.3535.1125002...@python.org>,
Steve Holden <st...@holdenweb.com> wrote:

> rafi wrote:
> > Reinhold Birkenfeld wrote:
> >
> >
> >>>> exec(eval("'a%s=%s' % (count, value)"))
> >>>
> >>>why using the eval?
> >>>
> >>>exec ('a%s=%s' % (count, value))
> >>>
> >>>should be fine
> >>
> >>And this demonstrates why exec as a statement was a mistake ;)
> >>
> >>It actually is
> >>
> >>exec 'a%s=%s' % (count, value)
> >
> >
> > Noted.
> >
> > In the meantime another question I cannot find an answer to: any idea
> > why does eval() consider '=' as a syntax error?
> >
> > >>> eval ('a=1')
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in ?
> > File "<string>", line 1
> > a=1
> > ^
> > SyntaxError: invalid syntax
> >
> > Thanks
> >
> Because eval() takes an expression as an argument, and assignment is a
> statement.

And if you find this distinction annoying, try Lisp.

rg

Nx

unread,
Aug 25, 2005, 9:50:54 PM8/25/05
to
Thanks for all the responses and animated discussion,
which is still the best way to learn something new.

Most of the time it is not that you want to do something
in a certain way , it rather is one cannot think of a
better , faster more efficient way .

Nx

Adriaan Renting

unread,
Aug 26, 2005, 2:56:15 AM8/26/05
to pytho...@python.org
Not in my Python.

>>> for count in range(0, 10):
... value = count
... exec("'a%s=%s' % (count, value)")
...
>>> dir()
['__builtins__', '__doc__', '__name__', 'count', 'value']
>>> for count in range(0, 10):
... value = count
... exec(eval("'a%s=%s' % (count, value)"))
...
>>> dir()
['__builtins__', '__doc__', '__name__', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'count', 'value']
>>>

I myself use code like this to load user defined classes.
exec(eval("'from %s import %s' % (script, script)"))
exec(eval("'self.user_class = %s()' % script"))
self.user_class.run()

But this can probably be done with the imp module too.

>>>rafi <ra...@free.fr> 08/25/05 6:03 pm >>>

Adriaan Renting wrote:
>You might be able to do something along the lines of
>
>for count in range(0,maxcount):
> value = values[count]

> exec(eval("'a%s=%s' % (count, value)"))

why using the eval?

exec ('a%s=%s' % (count, value))

should be fine

--
rafi

"Imagination is more important than knowledge."
(Albert Einstein)

--
http://mail.python.org/mailman/listinfo/python-list

rafi

unread,
Aug 26, 2005, 3:26:01 AM8/26/05
to
Ron Garret wrote:

>>Because eval() takes an expression as an argument, and assignment is a
>>statement.
>
> And if you find this distinction annoying, try Lisp.

that's were I come from :-)

"Martin v. Löwis"

unread,
Aug 26, 2005, 4:19:03 AM8/26/05
to Adriaan Renting
Adriaan Renting wrote:
> Not in my Python.
>
>
>>>>for count in range(0, 10):
>
> ... value = count
> ... exec("'a%s=%s' % (count, value)")
> ...
>
>>>>dir()
>
> ['__builtins__', '__doc__', '__name__', 'count', 'value']

You did not copy the suggestion properly:

>>> for count in range(0, 10):

... exec 'a%s = %s' % (count,count)
...
>>> dir()
['__builtins__', '__doc__', '__file__', '__name__', 'a0', 'a1', 'a2',
'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'count']
>>> a5
5

(you can put additional parentheses around the string, but not
additional quotation marks)

Regards,
Martin

Adriaan Renting

unread,
Aug 26, 2005, 4:48:26 AM8/26/05
to pytho...@python.org
I was responding to rafi's suggestion, I had not received the "exec 'a%s = %s' % (count,count)" response yet at that time. The "exec 'a%s = %s' % (count,value)" works fine.

>Not in my Python.
>
>---snip---


>
>why using the eval?
>
>exec ('a%s=%s' % (count, value))
>
>should be fine
>
>--
>rafi

--- I appologize for any top-posting, and improper inlining, I'm using groupwise ---

>>>"Martin v. Löwis" <mar...@v.loewis.de> 08/26/05 10:19 am >>>

--
http://mail.python.org/mailman/listinfo/python-list


Peter Maas

unread,
Aug 26, 2005, 5:57:10 AM8/26/05
to
Benji York schrieb:

>> >>> suffix = 'var'
>> >>> vars()['a%s' % suffix] = 45
>> >>> avar
>> 45
>
>
> Quoting from http://docs.python.org/lib/built-in-funcs.html#l2h-76 about
> the "vars" built in:
>
> The returned dictionary should not be modified: the effects on the
> corresponding symbol table are undefined.

I tried this once and it worked. This may be too naive, so thanks
for the warning :)

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------

Robert Kern

unread,
Aug 26, 2005, 4:24:40 AM8/26/05
to pytho...@python.org
Adriaan Renting wrote:
> Not in my Python.
>
>
>>>>for count in range(0, 10):
>
> ... value = count
> ... exec("'a%s=%s' % (count, value)")

But that's not what rafi suggested.

rafi:


> why using the eval?
>
> exec ('a%s=%s' % (count, value))

See the difference?

Steve Holden

unread,
Aug 26, 2005, 10:59:06 AM8/26/05
to pytho...@python.org

Yes. A large part of learning a language is discovering the many idioms
that have already been established for doing certain things. These are a
kind of shorthand, established by long convention, that allow one to
avoid the "learning-by-use" curve. When I first came to Python that was
a very rewarding aspect of this newsgroup.

To this day it remains one of the friendliest tech-spots in cyberspace.

Mike Meyer

unread,
Aug 26, 2005, 8:24:56 PM8/26/05
to
[The context is totally hosed by top posting and failure to mark
quoted text. I gave up on recovering it.]

"Adriaan Renting" <ren...@astron.nl> writes:

> Not in my Python.
>
>>>> for count in range(0, 10):
> ... value = count
> ... exec("'a%s=%s' % (count, value)")

You left in the extra set of quotes. Try this:

>>> for count in range(10):
... value = count
... exec 'a%s = %s' % (count, value)
...
>>> dir()
['__builtins__', '__doc__', '__file__', '__name__', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'count', 'help', 'readline', 'rlcompleter', 'sys', 'value']

I also removed the extraneous parens that you and Rafi both used -
exec is a statement, not a function.

Of course, your two examples are better done using imp and globals() or
locals().

<mike

--
Mike Meyer <m...@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

Adriaan Renting

unread,
Aug 29, 2005, 3:17:39 AM8/29/05
to pytho...@python.org
I'm sorry for top-posting and not marking quoted text, but my e-mail reader (Novell Groupwise 6 for Linux) does not mark quoted text. The only thing it does is the >>> $NAME <$EMAIL> $DATE >>> above the quoted text. Therefore I add my comments at the top. The only alternative I have is copying all text to an editor, adding quote marks by hand, then copying it back into a new e-mail message, but this takes to much of m time. If this is to confusing I will just stop posting in this list.

My original code was: exec(eval("'a%s=%s' % (count, value)"))
Then Rafi said: exec("'a%s=%s' % (count, value)")
To which I responded that his example does not work in my Python, and I think it's invalid.
Then Martin came with: exec 'a%s = %s' % (count, value)
This does work.
But he seems to keep telling me I'm quoting him wrong, while I'm quoting Rafi.

Furthermore I'm going with your suggestion of checking the imp, globals() and locals().

Adriaan Renting.

>>>Mike Meyer <m...@mired.org> 08/27/05 2:24 am >>>

--
http://mail.python.org/mailman/listinfo/python-list

Robert Kern

unread,
Aug 29, 2005, 5:13:17 AM8/29/05
to pytho...@python.org
Adriaan Renting wrote:

> My original code was: exec(eval("'a%s=%s' % (count, value)"))
> Then Rafi said: exec("'a%s=%s' % (count, value)")
> To which I responded that his example does not work in my Python, and I think it's invalid.
> Then Martin came with: exec 'a%s = %s' % (count, value)
> This does work.
> But he seems to keep telling me I'm quoting him wrong, while I'm quoting Rafi.

Read more carefully. rafi did not put in the extraneous quotes.

rafi:


>>Adriaan Renting wrote:
>>
>>>You might be able to do something along the lines of
>>>
>>>for count in range(0,maxcount):
>>>value = values[count]
>>>exec(eval("'a%s=%s' % (count, value)"))
>>
>>why using the eval?
>>
>>exec ('a%s=%s' % (count, value))
>>
>>should be fine
>>
>>--
>>rafi

See? No extra quotes.

Adriaan Renting

unread,
Aug 29, 2005, 10:07:20 AM8/29/05
to pytho...@python.org
Oops, you're completely right.

I'm sorry, it is indeed my mistake.
I was thinking people were telling me I was quoting Martin wrong, while I was quoting Rafi wrong. I'll promise not quote anybody anymore until I have an e-mail program that can quote/copy properly. As reading it in one window, then typing it in another to test it, then copying it back, obviously introduced a PEBKAC error.

Adriaan Renting

Fredrik Lundh

unread,
Aug 30, 2005, 7:48:10 AM8/30/05
to pytho...@python.org
Steve Holden wrote:

> Yes. A large part of learning a language is discovering the many idioms
> that have already been established for doing certain things. These are a
> kind of shorthand, established by long convention, that allow one to
> avoid the "learning-by-use" curve.

it's not obvious that the OP has found the pydiomatic way to solve
his problem, though, given that most of this thread have been spent
on playing with exec, and that Robert Kern's question from the be-
ginning of this thread has still not been answered:

"Again, why unpack them into separate variables when they are *already*
in the form that you want to use them?"

</F>

Tim N. van der Leeuw

unread,
Aug 30, 2005, 8:52:04 AM8/30/05
to
What you could do is to create a class for this; fill it's __dict__
instance; and use custom getattr() / setattr() methods for accessing
a0, a1, a2 etc.

cheers,

--Tim

Steve Holden

unread,
Aug 30, 2005, 1:35:44 PM8/30/05
to pytho...@python.org
Fair enough. I was trying to advocate the use of the suggested idioms,
but some people seem to insist on going their own way even when it leads
to a collision wiht a bricj wall.

You can lead an idiot to idioms, but you can't make him think ;-)

sp1...@gmail.com

unread,
Aug 30, 2005, 4:47:38 PM8/30/05
to
Steve, can I quote you on that?

"You can lead an idiot to idioms, but you can't make him think!" --
Steve Holden 2005

Steve Holden

unread,
Aug 30, 2005, 5:49:16 PM8/30/05
to pytho...@python.org
Looks like you just did :-). Feel free.
0 new messages