setattr and getattr, when to use?

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

    setattr and getattr, when to use?

    Why are these functions there? Is it somehow more idiomatic to use
    than to do obj.field ?
    Is there something you can with them that you can't by obj.field
    reference?
  • Jason Scheirer

    #2
    Re: setattr and getattr, when to use?

    On Aug 22, 8:50 pm, maestro <notnorweg...@y ahoo.sewrote:
    Why are these functions there? Is it somehow more idiomatic to use
    than to do obj.field ?
    Is there something you can with them that you can't by obj.field
    reference?
    You can generate them dynamically from strings. In some cases you
    don't know until runtime what attributes you want to pull:

    def show_insides(ob j):
    for attr in dir(obj):
    print "Attribute %r: %r" % (attr, getattr(obj, attr))

    class hello(object):
    a = 1
    b = 2

    class goodbye(object) :
    c = 1
    d = 500


    print show_insides(he llo)
    (...15 builtins...)
    Attribute 'a': 1
    Attribute 'b': 2

    print show_insides(go odbye)
    (...15 builtins...)
    Attribute 'c': 1
    Attribute 'd': 500

    In this case, you can see that we pull the attributes of an object
    using dir(), which yields a list of strings, then pull each attribute
    we discover.

    Comment

    • Jason Scheirer

      #3
      Re: setattr and getattr, when to use?

      On Aug 22, 10:17 pm, Jason Scheirer <jason.schei... @gmail.comwrote :
      On Aug 22, 8:50 pm, maestro <notnorweg...@y ahoo.sewrote:
      >
      Why are these functions there? Is it somehow more idiomatic to use
      than to do obj.field ?
      Is there something you can with them that you can't by obj.field
      reference?
      >
      You can generate them dynamically from strings. In some cases you
      don't know until runtime what attributes you want to pull:
      >
      def show_insides(ob j):
        for attr in dir(obj):
          print "Attribute %r: %r" % (attr, getattr(obj, attr))
      >
      class hello(object):
         a = 1
         b = 2
      >
      class goodbye(object) :
         c = 1
         d = 500
      >
      print show_insides(he llo)
      (...15 builtins...)
      Attribute 'a': 1
      Attribute 'b': 2
      >
      print show_insides(go odbye)
      (...15 builtins...)
      Attribute 'c': 1
      Attribute 'd': 500
      >
      In this case, you can see that we pull the attributes of an object
      using dir(), which yields a list of strings, then pull each attribute
      we discover.
      Might I add: avoid doing this if you don't have to. obj.field is the
      way to go about getting your object attributes 95% of the time. The 5%
      of your time when you are doing metaprogramming or other abuses of the
      object system are when you use get/setattr.

      Comment

      • Steven D'Aprano

        #4
        Re: setattr and getattr, when to use?

        On Fri, 22 Aug 2008 20:50:17 -0700, maestro wrote:
        Why are these functions there? Is it somehow more idiomatic to use than
        to do obj.field ?

        Heavens no!!! Using setattr and getattr is *less* idiomatic.

        Is there something you can with them that you can't by obj.field
        reference?
        Absolutely. Consider:

        class Foo(object):
        pass

        foo = Foo()
        setattr(foo, "x y", 1)
        print getattr(foo, "x y")

        That's probably an abuse of setattr/getattr, but there are times where
        you might not know the name of the attribute until runtime, so you can't
        write foo.attr since you don't know what to write in place of attr.
        That's where setattr and getattr (as well as delattr and hasattr) come
        into play.



        --
        Steven

        Comment

        • Bruno Desthuilliers

          #5
          Re: setattr and getattr, when to use?

          Jason Scheirer a écrit :
          (snip)
          The 5%
          of your time when you are doing metaprogramming or other abuses of the
          object system are when you use get/setattr.
          What makes you label "metaprogrammin g" and get/setattr as "abuses" ???

          Comment

          Working...