By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Variable Variable

P: n/a
Hi List, is there in python a variable variable like in PHP ($$var)?

What I want to do is something like that:

pc=["a","b","c"]

for i in pc:
i = anyclass()

a.shutdown()
b.update()
Any Ideas?

Many Thanks, m
Jul 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Tanteauguri wrote:
Hi List, is there in python a variable variable like in PHP ($$var)?

What I want to do is something like that:

pc=["a","b","c"]

for i in pc:
i = anyclass()

a.shutdown()
b.update()


Use a dictionary:

stuff = {}
pc = ['a', 'b', 'c']

for i in pc:
stuff[i] = anyclass()

stuff['a'].shutdown()
stuff['b'].update()
Jul 18 '05 #2

P: n/a
On Sat, 19 Mar 2005 04:35:47 -0500, Leif K-Brooks <eu*****@ecritters.biz> wrote:
Tanteauguri wrote:
Hi List, is there in python a variable variable like in PHP ($$var)?

What I want to do is something like that:

pc=["a","b","c"]

for i in pc:
i = anyclass()

a.shutdown()
b.update()
Use a dictionary:

stuff = {}
pc = ['a', 'b', 'c']


I think what he wants to do is basically hold object names in a tuple
list, and then call the methods on those objects (while iterating or
whatever).

for i in pc:
stuff[i] = anyclass()

stuff['a'].shutdown()
stuff['b'].update()
--
http://mail.python.org/mailman/listinfo/python-list

--
Premshree Pillai
http://www.livejournal.com/users/premshree/
Jul 18 '05 #3

P: n/a

Tanteauguri wrote:
Hi List, is there in python a variable variable like in PHP ($$var)?

What I want to do is something like that:

pc=["a","b","c"]

for i in pc:
i = anyclass()

a.shutdown()
b.update()
Any Ideas?


def seq(n,cls,*args,**kw):
"create a sequence of n objects of type cls."
return [cls(*args,**kw) for i in range(n)]
a,b,c = seq(3,anyclass)
a.shutdown()
b.update()


Regards Kay

Jul 18 '05 #4

P: n/a
In article <11**********************@g14g2000cwa.googlegroups .com>,
Kay Schluehr <ka**********@gmx.net> wrote:

Tanteauguri wrote:
Hi List, is there in python a variable variable like in PHP ($$var)?

What I want to do is something like that:

pc=["a","b","c"]

for i in pc:
i = anyclass()

a.shutdown()
b.update()
Any Ideas?


def seq(n,cls,*args,**kw):
"create a sequence of n objects of type cls."
return [cls(*args,**kw) for i in range(n)]
a,b,c = seq(3,anyclass)
a.shutdown()
b.update()


Regards Kay


I'm going to make this explicit: the PHP idiom is a defect.
Yes, I know that's good style among top PHP practitioners,
but, from all I know, it's simply a bad habit. The advice to
use a dictionary is on target.
Jul 18 '05 #5

P: n/a
Tanteauguri a écrit :
Hi List, is there in python a variable variable like in PHP ($$var)?


Hopefully, no.

See other answers in that thread for pythonic idioms.
Jul 18 '05 #6

P: n/a
> I'm going to make this explicit: the PHP idiom is a defect.
Yes, I know that's good style among top PHP practitioners,
but, from all I know, it's simply a bad habit. The advice to
use a dictionary is on target.


I tried to like it the whole last hour ;-)

I reconstructed the formal structure of $$var from a PHP Web-tutorial
though i do not have any experience with the language.

This is what i got:

$ =
<Name> ---> <Var> --------------> <Var>x<String>
| |
$ | # | $ o q' o proj2
| |
V V
<Var> ----- f ------> <Var>
| |
| # | =
| V
+-----------------> <Var>x<String>
=

In the diagram $ is an operator that creates a var from a name, = is an
operator that binds a string to a var and q' drops quotations from a
string to get an name. The function f is an implied isomorphism. It
represents the equivalence of $$a and $b, where $a = "b".

Personally I could live with that, but the diagram is a bit special
because of the restriction of the = operation. I do not know if PHP
supports this operational view by enabling iterations: $a, $$a, $$$a
.... ?

After all I can also live without that in Python...

Regards Kay

Jul 18 '05 #7

P: n/a
>
Personally I could live with that, but the diagram is a bit special
because of the restriction of the = operation. I do not know if PHP
supports this operational view by enabling iterations: $a, $$a, $$$a
... ?


It does.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #8

P: n/a
Premshree Pillai wrote:
On Sat, 19 Mar 2005 04:35:47 -0500, Leif K-Brooks <eu*****@ecritters.biz> wrote:
Tanteauguri wrote:
Hi List, is there in python a variable variable like in PHP ($$var)?

What I want to do is something like that:

pc=["a","b","c"]

for i in pc:
i = anyclass()

a.shutdown()
b.update()


Use a dictionary:

stuff = {}
pc = ['a', 'b', 'c']

I think what he wants to do is basically hold object names in a tuple
list, and then call the methods on those objects (while iterating or
whatever).

for i in pc:
stuff[i] = anyclass()

stuff['a'].shutdown()
stuff['b'].update()
--
http://mail.python.org/mailman/listinfo/python-list


Try this:

pc=["a","b","c"]
for i in pc:
vars()[i] = anyclass()
a.shutdown()
b.update()

MikeG

Jul 18 '05 #9

P: n/a
Premshree Pillai wrote:
On Sat, 19 Mar 2005 04:35:47 -0500, Leif K-Brooks <eu*****@ecritters.biz> wrote:
Tanteauguri wrote:
Hi List, is there in python a variable variable like in PHP ($$var)?

What I want to do is something like that:

pc=["a","b","c"]

for i in pc:
i = anyclass()

a.shutdown()
b.update()


Use a dictionary:

stuff = {}
pc = ['a', 'b', 'c']

I think what he wants to do is basically hold object names in a tuple
list, and then call the methods on those objects (while iterating or
whatever).

for i in pc:
stuff[i] = anyclass()

stuff['a'].shutdown()
stuff['b'].update()
--
http://mail.python.org/mailman/listinfo/python-list


Try this:

pc=["a","b","c"]
for i in pc:
vars()[i] = anyclass()
a.shutdown()
b.update()

MikeG

Jul 18 '05 #10

P: n/a
Premshree Pillai wrote:
On Sat, 19 Mar 2005 04:35:47 -0500, Leif K-Brooks <eu*****@ecritters.biz> wrote:
Tanteauguri wrote:
Hi List, is there in python a variable variable like in PHP ($$var)?

What I want to do is something like that:

pc=["a","b","c"]

for i in pc:
i = anyclass()

a.shutdown()
b.update()


Use a dictionary:

stuff = {}
pc = ['a', 'b', 'c']

I think what he wants to do is basically hold object names in a tuple
list, and then call the methods on those objects (while iterating or
whatever).

for i in pc:
stuff[i] = anyclass()

stuff['a'].shutdown()
stuff['b'].update()
--
http://mail.python.org/mailman/listinfo/python-list


Try this:

pc=["a","b","c"]
for i in pc:
vars()[i] = anyclass()
a.shutdown()
b.update()

MikeG
Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.