FAQ
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

Search Discussions

  • Chris Rebert at Sep 4, 2008 at 7:36 am

    On Thu, Sep 4, 2008 at 12:25 AM, Mathieu Prevot 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
  • Mathieu Prevot at Sep 4, 2008 at 7:59 am

    2008/9/4 Chris Rebert <cvrebert at gmail.com>:
    On Thu, Sep 4, 2008 at 12:25 AM, Mathieu Prevot
    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 at Sep 4, 2008 at 8:05 am

    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 at Sep 4, 2008 at 8:33 am

    Mathieu Prevot a ?crit :
    2008/9/4 Chris Rebert (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 ?
  • Fredrik Lundh at Sep 4, 2008 at 8:47 am

    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>
  • Mathieu Prevot at Sep 4, 2008 at 1:01 pm

    2008/9/4 Fredrik Lundh <fredrik at 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
  • Bruno Desthuilliers at Sep 5, 2008 at 7:16 pm

    Marco Bizzarri a ?crit :
    On Thu, Sep 4, 2008 at 10:47 AM, Fredrik Lundh 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.
  • Marco Bizzarri at Sep 6, 2008 at 6:02 am

    On Fri, Sep 5, 2008 at 9:16 PM, Bruno Desthuilliers 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 :-)
  • Michele Simionato at Sep 6, 2008 at 6:39 am

    On Sep 6, 8:02?am, "Marco Bizzarri" wrote:
    On Fri, Sep 5, 2008 at 9:16 PM, Bruno Desthuilliers

    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#7764

    (as well as the comment below by P.J. Eby)
  • Marco Bizzarri at Sep 5, 2008 at 7:24 pm

    On Thu, Sep 4, 2008 at 10:47 AM, Fredrik Lundh 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?
  • Fredrik Lundh at Sep 6, 2008 at 5:52 am

    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 at Sep 6, 2008 at 6:08 am

    On Sat, Sep 6, 2008 at 7:52 AM, Fredrik Lundh 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?
  • Fredrik Lundh at Sep 6, 2008 at 10:58 am

    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>
  • Cameron Laird at Sep 4, 2008 at 12:02 pm
    In article <48bf9d12$0$7552$426a74cc at news.free.fr>,
    Bruno Desthuilliers wrote:
    Mathieu Prevot a ?crit :
    2008/9/4 Chris Rebert <cvrebert at 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 ?
    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.
  • Fredrik Lundh at Sep 4, 2008 at 7:36 am

    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 at Sep 4, 2008 at 7:47 am
    En Thu, 04 Sep 2008 04:25:37 -0300, Mathieu Prevot
    <mathieu.prevot at 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 ?

    Thanks,
    Mathieu
    --
    http://mail.python.org/mailman/listinfo/python-list


    --
    Gabriel Genellina
  • Nick Craig-Wood at Sep 4, 2008 at 8:35 am

    Mathieu Prevot 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 <nick at craig-wood.com> -- http://www.craig-wood.com/nick

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 4, '08 at 7:25a
activeSep 6, '08 at 10:58a
posts18
users10
websitepython.org

People

Translate

site design / logo © 2022 Grokbase