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
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
> 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>
> 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 ?
>
> Thanks,
> Mathieu
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
Gabriel Genellina
Indeed.
I'll use:
a.__setattr__(height, new_value)
Thanks to all
Mathieu
> I'll use:
> a.__setattr__(height, new_value)
that's an implementation detail. please use setattr() instead, like
everyone else.
</F>
(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 ?
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
> 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>
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.
Got it. Thanks :)
Mathieu
>
> (...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/
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.
>> (...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>
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 :-)
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?
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)
>>> 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>