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

use str as variable name

2 views
Skip to first unread message

Mathieu Prevot

unread,
Sep 4, 2008, 3:25:37 AM9/4/08
to python-list
Hi,

I have a program that take a word as argument, and I would like to
link this word to a class variable.

eg.
class foo():
width = 10
height = 20

a=foo()
arg='height'
a.__argname__= new_value

rather than :

if arg == 'height':
a.height = new_value
elif arg == 'width';
a.width = new_value

Can I do this with python ? How ?

Thanks,
Mathieu

Chris Rebert

unread,
Sep 4, 2008, 3:36:13 AM9/4/08
to Mathieu Prevot, python-list
On Thu, Sep 4, 2008 at 12:25 AM, Mathieu Prevot
<mathieu...@gmail.com> wrote:
> Hi,
>
> I have a program that take a word as argument, and I would like to
> link this word to a class variable.
>
> eg.
> class foo():

You should subclass 'object', so that should be:
class Foo(object):

> width = 10
> height = 20
>
> a=foo()
> arg='height'
> a.__argname__= new_value

You're looking for the setattr() built-in function. In this exact case:
setattr(a, arg, new_value)

This is probably covered in the Python tutorial, please read it.

Regards,
Chris

>
> rather than :
>
> if arg == 'height':
> a.height = new_value
> elif arg == 'width';
> a.width = new_value
>
> Can I do this with python ? How ?
>
> Thanks,
> Mathieu

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

--
Follow the path of the Iguana...
http://rebertia.com

Fredrik Lundh

unread,
Sep 4, 2008, 3:36:29 AM9/4/08
to pytho...@python.org
Mathieu Prevot wrote:

> I have a program that take a word as argument, and I would like to
> link this word to a class variable.
>
> eg.
> class foo():

> width = 10
> height = 20
>
> a=foo()
> arg='height'
> a.__argname__= new_value
>

> rather than :
>
> if arg == 'height':
> a.height = new_value
> elif arg == 'width';
> a.width = new_value
>
> Can I do this with python ? How ?

assuming you mean "instance variable" ("a" is an instance of the class
"foo"), you can use setattr:

a = foo()
arg = 'height'
setattr(a, arg, new_value)

</F>

Gabriel Genellina

unread,
Sep 4, 2008, 3:47:07 AM9/4/08
to pytho...@python.org
En Thu, 04 Sep 2008 04:25:37 -0300, Mathieu Prevot
<mathieu...@gmail.com> escribi�:

> I have a program that take a word as argument, and I would like to
> link this word to a class variable.
>
> eg.
> class foo():
> width = 10
> height = 20
>
> a=foo()
> arg='height'
> a.__argname__= new_value
>
> rather than :
>
> if arg == 'height':
> a.height = new_value
> elif arg == 'width';
> a.width = new_value

You're looking for "setattr":

setattr(a, arg, new_value)

http://docs.python.org/lib/built-in-funcs.html#l2h-66

>
> Can I do this with python ? How ?
>

--
Gabriel Genellina

Mathieu Prevot

unread,
Sep 4, 2008, 3:59:10 AM9/4/08
to Chris Rebert, python-list
2008/9/4 Chris Rebert <cvre...@gmail.com>:

> On Thu, Sep 4, 2008 at 12:25 AM, Mathieu Prevot
> <mathieu...@gmail.com> wrote:
>> Hi,
>>
>> I have a program that take a word as argument, and I would like to
>> link this word to a class variable.
>>
>> eg.
>> class foo():
>
> You should subclass 'object', so that should be:
> class Foo(object):
>
>> width = 10
>> height = 20
>>
>> a=foo()
>> arg='height'
>> a.__argname__= new_value
>
> You're looking for the setattr() built-in function. In this exact case:
> setattr(a, arg, new_value)
>
> This is probably covered in the Python tutorial, please read it.
>
> Regards,
> Chris

Indeed.

I'll use:
a.__setattr__(height, new_value)

Thanks to all
Mathieu

Fredrik Lundh

unread,
Sep 4, 2008, 4:05:20 AM9/4/08
to pytho...@python.org
Mathieu Prevot wrote:

> I'll use:
> a.__setattr__(height, new_value)

that's an implementation detail. please use setattr() instead, like
everyone else.

</F>

Bruno Desthuilliers

unread,
Sep 4, 2008, 4:33:58 AM9/4/08
to
Mathieu Prevot a écrit :
> 2008/9/4 Chris Rebert <cvre...@gmail.com>:

(snip)

>> You're looking for the setattr() built-in function. In this exact case:
>> setattr(a, arg, new_value)
>>
>> This is probably covered in the Python tutorial, please read it.
>>
>> Regards,
>> Chris
>
> Indeed.
>
> I'll use:
> a.__setattr__(height, new_value)

Please don't. Use the generic setattr() function instead. This holds for
any __magic__ method : they are *implementation* for operators and
generic functions - which you can think of as operators with a function
syntax -, and are not meant to be called directly. You wouldn't write
something like 2.__add__(3), would you ?

Nick Craig-Wood

unread,
Sep 4, 2008, 4:35:52 AM9/4/08
to
Mathieu Prevot <mathieu...@gmail.com> wrote:
> Hi,
>
> I have a program that take a word as argument, and I would like to
> link this word to a class variable.
>
> eg.
> class foo():
> width = 10
> height = 20
>
> a=foo()
> arg='height'
> a.__argname__= new_value

Not quite sure what the above is supposed to achieve

> rather than :
>
> if arg == 'height':
> a.height = new_value
> elif arg == 'width';
> a.width = new_value
>
> Can I do this with python ? How ?

setattr(a, arg, new_value)

See: http://docs.python.org/lib/built-in-funcs.html


--
Nick Craig-Wood <ni...@craig-wood.com> -- http://www.craig-wood.com/nick

Fredrik Lundh

unread,
Sep 4, 2008, 4:47:04 AM9/4/08
to pytho...@python.org
Bruno Desthuilliers wrote:

> You wouldn't write something like 2.__add__(3), would you ?

Don't give the "it's only OO if I write obj.method(args)" crowd more bad
ideas, please ;-)

(...as Bruno implies, setattr(), len() et al can be and should be viewed
as generic functions. A specific Python implementation may use custom
code to implement behaviour for a given object; behaviour that's more
efficient than a full Python-level method call. For example, in
CPython, len(L) is about twice as fast as L.__len__() for built-in
sequences.)

</F>

Cameron Laird

unread,
Sep 4, 2008, 8:02:08 AM9/4/08
to
In article <48bf9d12$0$7552$426a...@news.free.fr>,

Along with the good advice the usual suspects have given,
my intuition is that there's an even better implementation
that doesn't setattr() at all. While it's impossible to
know, of course, because we don't have the original poster's
true requirements, I conjecture that, rather than "to link
this [user-supplied] word to a class variable", what will
serve him best is to regard the user text as an index into
a class dictionary.

Mathieu Prevot

unread,
Sep 4, 2008, 9:01:49 AM9/4/08
to Fredrik Lundh, pytho...@python.org
2008/9/4 Fredrik Lundh <fre...@pythonware.com>:

> Bruno Desthuilliers wrote:
>
>> You wouldn't write something like 2.__add__(3), would you ?
>
> Don't give the "it's only OO if I write obj.method(args)" crowd more bad
> ideas, please ;-)
>
> (...as Bruno implies, setattr(), len() et al can be and should be viewed as
> generic functions. A specific Python implementation may use custom code to
> implement behaviour for a given object; behaviour that's more efficient than
> a full Python-level method call. For example, in CPython, len(L) is about
> twice as fast as L.__len__() for built-in sequences.)

Got it. Thanks :)
Mathieu

Marco Bizzarri

unread,
Sep 5, 2008, 3:24:37 PM9/5/08
to pytho...@python.org
On Thu, Sep 4, 2008 at 10:47 AM, Fredrik Lundh <fre...@pythonware.com> wrote:

>
> (...as Bruno implies, setattr(), len() et al can be and should be viewed as
> generic functions.

Just a question: "generic functions" are not meant in the sense of
"generic functions" of CLOS, am I right?

--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/

Bruno Desthuilliers

unread,
Sep 5, 2008, 3:16:36 PM9/5/08
to
Marco Bizzarri a écrit :

> On Thu, Sep 4, 2008 at 10:47 AM, Fredrik Lundh <fre...@pythonware.com> wrote:
>
>> (...as Bruno implies, setattr(), len() et al can be and should be viewed as
>> generic functions.
>
> Just a question: "generic functions" are not meant in the sense of
> "generic functions" of CLOS, am I right?

Nope. Just "generic" in the sense that they accept any object
implementing a very minimal interface.

If you want something like CLOS multimethods, you may be interested in
Philip Eby's ruledispatch.

Fredrik Lundh

unread,
Sep 6, 2008, 1:52:41 AM9/6/08
to pytho...@python.org
Marco Bizzarri wrote:

>> (...as Bruno implies, setattr(), len() et al can be and should be viewed as
>> generic functions.
>
> Just a question: "generic functions" are not meant in the sense of
> "generic functions" of CLOS, am I right?

it's meant in exactly that sense: len(L) means "of all len()
implementations available to the runtime, execute the most specific code
we have for the object L".

</F>

Marco Bizzarri

unread,
Sep 6, 2008, 2:02:28 AM9/6/08
to pytho...@python.org
On Fri, Sep 5, 2008 at 9:16 PM, Bruno Desthuilliers
<bdesth.qu...@free.quelquepart.fr> wrote:
> Marco Bizzarri a écrit :

>>
>> Just a question: "generic functions" are not meant in the sense of
>> "generic functions" of CLOS, am I right?
>
> Nope. Just "generic" in the sense that they accept any object implementing a
> very minimal interface.
>
> If you want something like CLOS multimethods, you may be interested in
> Philip Eby's ruledispatch.
>

Even though I loved them when I used at university, I'm not looking
for them right now... but nice to know that they are available under
python :-)

Marco Bizzarri

unread,
Sep 6, 2008, 2:08:29 AM9/6/08
to python-list
On Sat, Sep 6, 2008 at 7:52 AM, Fredrik Lundh <fre...@pythonware.com> wrote:
> Marco Bizzarri wrote:
>
>>> (...as Bruno implies, setattr(), len() et al can be and should be viewed
>>> as
>>> generic functions.
>>
>> Just a question: "generic functions" are not meant in the sense of
>> "generic functions" of CLOS, am I right?
>
> it's meant in exactly that sense: len(L) means "of all len() implementations
> available to the runtime, execute the most specific code we have for the
> object L".
>

It is a generic functions like a CLOS one, as long as we remain to one
parameter.

I mean, there will be just one implemenatation of

foo(bar, man)

which the python interpretr can find; am I right?

Michele Simionato

unread,
Sep 6, 2008, 2:39:02 AM9/6/08
to
On Sep 6, 8:02 am, "Marco Bizzarri" <marco.bizza...@gmail.com> wrote:
> On Fri, Sep 5, 2008 at 9:16 PM, Bruno Desthuilliers
>
> <bdesth.quelquech...@free.quelquepart.fr> wrote:
> > Marco Bizzarri a écrit :
>
> >> Just a question: "generic functions" are not meant in the sense of
> >> "generic functions" of CLOS, am I right?
>
> > Nope. Just "generic" in the sense that they accept any object implementing a
> > very minimal interface.
>
> > If you want something like CLOS multimethods, you may be interested in
> > Philip Eby's ruledispatch.
>
> Even though I loved them when I used at university, I'm not looking
> for them right now... but nice to know that they are available under
> python :-)

Actually they are already available in the standard library but they
are undocumented. See for instance
this recent blog post of mine:

http://www.artima.com/weblogs/viewpost.jsp?thread=237764

(as well as the comment below by P.J. Eby)

Fredrik Lundh

unread,
Sep 6, 2008, 6:58:37 AM9/6/08
to pytho...@python.org
Marco Bizzarri wrote:

>>> Just a question: "generic functions" are not meant in the sense of
>>> "generic functions" of CLOS, am I right?
>>

>> it's meant in exactly that sense: len(L) means "of all len() implementations
>> available to the runtime, execute the most specific code we have for the
>> object L".
>>
>
> It is a generic functions like a CLOS one, as long as we remain to one
> parameter.
>
> I mean, there will be just one implemenatation of
>
> foo(bar, man)
>
> which the python interpretr can find; am I right?

Let's see if I can sort this out without causing even more confusion.

The Python *language* doesn't support generic functions in the CLOS
sense, but a given Python *implementation* may use a dispatching
machinery to select the best possible implementation for any call to a
built-in function.

Or in other words, the len() function shouldn't just be seen as a
function that *always* does

def len(L):
return L.__len__()

because if you look under the covers, it might be more like (using a
hypothetical Python dialect):

def generic len(L: list):
return list::get_size(L) # fast internal dispatch

def generic len(L: tuple):
return tuple::get_size(L) # fast internal dispatch

def generic len(L: object):
return L.__len__() # fallback behaviour, using method dispatch

where "len" represents a plurality of possible "len" implementations.

How the dispatching is actually done is up to the specific Python
implementation; CPython, for example, offers a "slot" mechanism for
types implemented in C that's quite a bit faster than the method call
machinery. And the slot mechanism isn't always single dispatch. For
example, internal getattr(obj, name) calls use one of several slots,
depending on what "name" is.

Other Python implementations may use different approaches, but the point
remains: a builtin function "operation(a, b, c)" isn't always mapped to
"a.__operation__(b, c)" by the runtime; code that uses the latter form
may be less efficient. And it's definitely less Pythonic.

</F>

0 new messages