Re: use str as variable name

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mathieu Prevot

    Re: use str as variable name

    2008/9/4 Chris Rebert <cvrebert@gmail .com>:
    On Thu, Sep 4, 2008 at 12:25 AM, Mathieu Prevot
    <mathieu.prevot @gmail.comwrote :
    >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__(h eight, new_value)

    Thanks to all
    Mathieu
  • Bruno Desthuilliers

    #2
    Re: use str as variable name

    Mathieu Prevot a écrit :
    2008/9/4 Chris Rebert <cvrebert@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__(h eight, 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 ?

    Comment

    • Fredrik Lundh

      #3
      Re: use str as variable name

      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>

      Comment

      • Cameron Laird

        #4
        Re: use str as variable name

        In article <48bf9d12$0$755 2$426a74cc@news .free.fr>,
        Bruno Desthuilliers <bruno.42.desth uilliers@websit eburo.invalidwr ote:
        >Mathieu Prevot a écrit :
        >2008/9/4 Chris Rebert <cvrebert@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.

        Comment

        • Mathieu Prevot

          #5
          Re: use str as variable name

          2008/9/4 Fredrik Lundh <fredrik@python ware.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

          Comment

          • Marco Bizzarri

            #6
            Re: use str as variable name

            On Thu, Sep 4, 2008 at 10:47 AM, Fredrik Lundh <fredrik@python ware.comwrote:
            >
            (...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
            Where we talk about coding, Dungeons and Dragons, and stuff.


            Comment

            • Fredrik Lundh

              #7
              Re: use str as variable name

              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>

              Comment

              • Marco Bizzarri

                #8
                Re: use str as variable name

                On Fri, Sep 5, 2008 at 9:16 PM, Bruno Desthuilliers
                <bdesth.quelque chose@free.quel quepart.frwrote :
                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
                Where we talk about coding, Dungeons and Dragons, and stuff.


                Comment

                • Marco Bizzarri

                  #9
                  Re: use str as variable name

                  On Sat, Sep 6, 2008 at 7:52 AM, Fredrik Lundh <fredrik@python ware.comwrote:
                  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?

                  --
                  Marco Bizzarri
                  Where we talk about coding, Dungeons and Dragons, and stuff.


                  Comment

                  • Michele Simionato

                    #10
                    Re: use str as variable name

                    On Sep 6, 8:02 am, "Marco Bizzarri" <marco.bizza... @gmail.comwrote :
                    On Fri, Sep 5, 2008 at 9:16 PM, Bruno Desthuilliers
                    >
                    <bdesth.quelque ch...@free.quel quepart.frwrote :
                    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:



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

                    Comment

                    • Fredrik Lundh

                      #11
                      Re: use str as variable name

                      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>

                      Comment

                      Working...