[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Variable definition

Raphael Mayoraz

2/26/2010 11:32:00 PM

Hello,

I'd like to define variables with some specific name that has a common
prefix.
Something like this:

varDic = {'red': 'a', 'green': 'b', 'blue': 'c'}
for key, value in varDic.iteritems():
'myPrefix' + key = value

I know this is illegal, but there must be a trick somewhere.

Thanks,

Raphael



5 Answers

Rhodri James

2/27/2010 12:15:00 AM

0

On Fri, 26 Feb 2010 23:32:27 -0000, Raphael Mayoraz <maygeo@netplus.ch>
wrote:

> I'd like to define variables with some specific name that has a common
> prefix.

Why?

No seriously, how do you think this is going to solve whatever problem you
clearly think it will solve?

--
Rhodri James *-* Wildebeeste Herder to the Masses

Alf P. Steinbach

2/27/2010 12:22:00 AM

0

* Raphael Mayoraz:
> Hello,
>
> I'd like to define variables with some specific name that has a common
> prefix.
> Something like this:
>
> varDic = {'red': 'a', 'green': 'b', 'blue': 'c'}
> for key, value in varDic.iteritems():
> 'myPrefix' + key = value
>
> I know this is illegal, but there must be a trick somewhere.

In general you'll IMHO be better off with the variables as attributes of an object.

If you want them to be modifiable then you can do

class Whatever: pass

myPrefix = Whatever()
myPrefix.a = 'a'
myPrefix.b = 'b'
myPrefix.c = 'c'

If you want them to be sort of constants (weasel words intentional) then you
might do (Python 3.x) -- disclaimer: off-the-cuff & I'm not sure if that
function is called 'namedtuple' but I think you'll find it anyway --

import collections

Color = namedtuple( "Color", "red green blue" )
myPrefix = Color( 'a', 'b', 'c' )


Cheers & hth.,

- Alf

Steven D'Aprano

2/27/2010 3:34:00 AM

0

On Fri, 26 Feb 2010 15:32:27 -0800, Raphael Mayoraz wrote:

> Hello,
>
> I'd like to define variables with some specific name that has a common
> prefix.
> Something like this:
>
> varDic = {'red': 'a', 'green': 'b', 'blue': 'c'}
> for key, value in varDic.iteritems():
> 'myPrefix' + key = value
>
> I know this is illegal, but there must be a trick somewhere.


The Right answer is:

Don't do that. How can you use a variable without knowing its name?

Suppose you do this:

for key, value in varDic.iteritems():
'myPrefix_' + key = value

and then later you want:

print myPrefix_blue

But hang on. How could you possibly know it is called myPrefix_blue and
not myPrefix_cyan or myPrefix_turquoise?

You can't possibly know, so in general defining a named variable is
useless. What you want instead is:

some_colour = 'blue' # get this at runtime
print varDic[some_colour]

You don't even need the myPrefix, but if you really want it:

for key, value in varDic.iteritems():
varDic['myPrefix_' + key] = value
del varDic[key]

some_colour = 'blue' # get this at runtime
print varDic['myPrefix_' + some_colour]

or any one of a million variations on this idea.

For some very, very rare situations you might need to programatically
define variables. The right way to do that is:

globals()['myPrefix_turquoise'] = 42

or

setattr(module, 'myPrefix_turquoise'] = 42

or even:

exec "'myPrefix_turquoise' = 42"

but if you are thinking of using that last one with data coming from an
untrusted user (and nearly all users are untrusted!), then DON'T. That is
a huge security hole. And it's also very slow.



--
Steven

Dave Angel

2/27/2010 4:56:00 AM

0



Steven D'Aprano wrote:
> <snip>
>
> for key, value in varDic.iteritems():
> varDic['myPrefix_' + key] = value
> del varDic[key]
>
> <snip>
Watch out if any of the existing values already startswith 'myPrefix'
You can end up with trouble just as confusing as if 'myPrefix' is an
empty string

DaveA

Andreas Waldenburger

2/27/2010 1:46:00 PM

0

On 27 Feb 2010 03:33:57 GMT Steven D'Aprano
<steve@REMOVE-THIS-cybersource.com.au> wrote:

> exec "'myPrefix_turquoise' = 42"
>
Not quite:

In [1]: exec "'myPrefix_turquoise' = 42"
------------------------------------------------------------
File "<string>", line 1
SyntaxError: can't assign to literal (<string>, line 1)


I think you meant:

exec "myPrefix_turquoise = 42"


/W

--
INVALID? DE!