Setting an attribute without calling __setattr__()

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

    Setting an attribute without calling __setattr__()

    OK, I'm sure the answer is staring me right in the face--whether that answer
    be "you can't do that" or "here's the really easy way--but I am stuck. I'm
    writing an object to proxy both lists (subscriptable iterables, really) and
    dicts.

    My init lookslike this:

    def __init__(self, obj=None):
    if type(obj).__nam e__ in 'list|tuple|set |frozenset':
    self.me = []
    for v in obj:
    self.me.append( ObjectProxy(v))
    elif type(obj) == dict:
    self.me = {}
    for k,v in obj.items():
    self.me[k] = ObjectProxy(v)

    and I have a __setattr__ defined like so:

    def __setattr__(sel f, name, value):
    self.me[name] = ObjectProxy(val ue)

    You can probably see the problem.

    While doing an init, self.me = {} or self.me = [] calls __setattr__, which
    then ends up in an infinite loop, and even it it succeeded

    self.me['me'] = {}

    is not what I wanted in the first place.

    Is there a way to define self.me without it firing __setattr__?

    If not, it's not a huge deal, as having this class read-only for now won't
    be a problem, but I was just trying to make it read/write.

    Thanks!

    j

  • Hrvoje Niksic

    #2
    Re: Setting an attribute without calling __setattr__()

    Joshua Kugler <jkugler@bigfoo t.comwrites:
    self.me = []
    self.me = {}
    Use "object.__setat tr__(self, 'me') = []" and likewise for {}.

    Comment

    • John Machin

      #3
      Re: Setting an attribute without calling __setattr__()

      On Apr 26, 7:01 am, Joshua Kugler <jkug...@bigfoo t.comwrote:
      OK, I'm sure the answer is staring me right in the face--whether that answer
      be "you can't do that" or "here's the really easy way--but I am stuck. I'm
      writing an object to proxy both lists (subscriptable iterables, really) and
      dicts.
      >
      My init lookslike this:
      >
      def __init__(self, obj=None):
      if type(obj).__nam e__ in 'list|tuple|set |frozenset':
      self.me = []
      for v in obj:
      self.me.append( ObjectProxy(v))
      elif type(obj) == dict:
      self.me = {}
      for k,v in obj.items():
      self.me[k] = ObjectProxy(v)
      >
      and I have a __setattr__ defined like so:
      >
      def __setattr__(sel f, name, value):
      self.me[name] = ObjectProxy(val ue)
      >
      You can probably see the problem.
      >
      While doing an init, self.me = {} or self.me = [] calls __setattr__, which
      then ends up in an infinite loop, and even it it succeeded
      >
      self.me['me'] = {}
      >
      is not what I wanted in the first place.
      >
      Is there a way to define self.me without it firing __setattr__?
      >
      Consider reading the *second* paragraph about __setattr__ in section
      3.4.2 of the Python Reference Manual.

      Comment

      • Hrvoje Niksic

        #4
        Re: Setting an attribute without calling __setattr__()

        Hrvoje Niksic <hniksic@xemacs .orgwrites:
        Joshua Kugler <jkugler@bigfoo t.comwrites:
        >
        > self.me = []
        > self.me = {}
        >
        Use "object.__setat tr__(self, 'me') = []" and likewise for {}.
        Oops, that should of course be "object.__setat tr__(self, 'me', [])".

        Comment

        • Joshua Kugler

          #5
          Re: Setting an attribute without calling __setattr__()

          John Machin wrote:
          >Is there a way to define self.me without it firing __setattr__?
          Consider reading the *second* paragraph about __setattr__ in section
          3.4.2 of the Python Reference Manual.
          Like I said in my original post, it was probably staring me right in the
          face. I had read through a bit of the documentation on special methods,
          but for some reason I missed that part.

          Thanks to all for your responses!

          j



          Comment

          • George Sakkis

            #6
            Re: Setting an attribute without calling __setattr__()

            On Apr 25, 5:01 pm, Joshua Kugler <jkug...@bigfoo t.comwrote:
            My init lookslike this:
            >
            def __init__(self, obj=None):
            if type(obj).__nam e__ in 'list|tuple|set |frozenset':
            self.me = []
            for v in obj:
            self.me.append( ObjectProxy(v))
            elif type(obj) == dict:
            self.me = {}
            for k,v in obj.items():
            self.me[k] = ObjectProxy(v)
            As an aside, unrelated to your question, Python encourages "duck
            typing" instead of exact type matching. Unless you have a good reason
            to restrict obj to one of the 5 types you hardcoded (a rather rare
            need), it is more flexible to write it as:

            def __init__(self, obj=None):
            if hasattr(obj, 'items'):
            # assume obj is a mapping type instance
            self.me = dict((k,ObjectP roxy(v)) for k,v in obj.items())
            else:
            try: # check if obj is an iterable instance
            self.me = map(ObjectProxy , obj)
            except TypeError:
            # handle other cases here
            # self.me = ...


            A downside of this flexibility is that it may be more liberal than it
            should. For instance, if obj just happens to have an 'items()' method
            but it's not really a mapping type, the assumption is violated. Python
            3 deals with such potential ambiguities by introducing Abstract Base
            Classes (ABCs) [1] that allow a class to make explicit its semantics.
            So in Py3K the hasattr() test above would rather be written as
            "isinstance(obj , Mapping)", where Mapping is the ABC that represents
            (read-only) mappings.

            A more difficult problem is that even if a class derives from some
            ABC, you may not always want to treat its instances as such. The
            typical gotcha is that strings are iterable, but in many (most?)
            applications they are to be treated as atomic values, not as sequences
            of characters. So in the example above if obj is a string, self.me
            will be a list of ObjectProxy instances, one per character; probably
            not what you intend. Of course we can check for "isinstance(obj ,str)"
            but then we're back at explicit type checking. There is no general way
            to express something lke "atomic value that also happens to be
            iterable (but pretend it's not)" because it's inherently domain-
            dependent.

            George

            [1] http://www.python.org/dev/peps/pep-3119/

            Comment

            • Arnaud Delobelle

              #7
              Re: Setting an attribute without calling __setattr__()

              Joshua Kugler <jkugler@bigfoo t.comwrites:

              [...]
              self.me = []
              for v in obj:
              self.me.append( ObjectProxy(v))
              Note that is could be spelt:

              self.me = map(ObjectProxy , v)

              --
              Arnaud

              Comment

              • animalMutha

                #8
                Re: Setting an attribute without calling __setattr__()



                Hrvoje Niksic wrote:
                Hrvoje Niksic <hniksic@xemacs .orgwrites:
                >
                Joshua Kugler <jkugler@bigfoo t.comwrites:
                self.me = []
                self.me = {}
                Use "object.__setat tr__(self, 'me') = []" and likewise for {}.
                >
                Oops, that should of course be "object.__setat tr__(self, 'me', [])".

                Comment

                • animalMutha

                  #9
                  Re: Setting an attribute without calling __setattr__()

                  Consider reading the *second* paragraph about __setattr__ in section
                  3.4.2 of the Python Reference Manual.
                  if you are simply going to answer rtfm - might as well kept it to
                  yourself.

                  Comment

                  • Aahz

                    #10
                    Re: Setting an attribute without calling __setattr__()

                    In article <m27ielb2i8.fsf @googlemail.com >,
                    Arnaud Delobelle <arnodel@google mail.comwrote:
                    >Joshua Kugler <jkugler@bigfoo t.comwrites:
                    >>
                    > self.me = []
                    > for v in obj:
                    > self.me.append( ObjectProxy(v))
                    >
                    >Note that is could be spelt:
                    >
                    >self.me = map(ObjectProxy , v)
                    It could also be spelt:

                    self.me = [ObjectProxy(v) for v in obj]

                    which is my preferred spelling....
                    --
                    Aahz (aahz@pythoncra ft.com) <* http://www.pythoncraft.com/

                    Why is this newsgroup different from all other newsgroups?

                    Comment

                    • Marc 'BlackJack' Rintsch

                      #11
                      Re: Setting an attribute without calling __setattr__()

                      On Sat, 26 Apr 2008 08:28:38 -0700, animalMutha wrote:
                      >Consider reading the *second* paragraph about __setattr__ in section
                      >3.4.2 of the Python Reference Manual.
                      >
                      if you are simply going to answer rtfm - might as well kept it to
                      yourself.
                      Yes, but if you are telling where exactly to find the wanted information
                      in the documentation, like John did, you are teaching the OP how to fish.
                      Which is a good thing. Much more helpful than your remark anyway. You
                      might as well have kept it to yourself. :-þ

                      Ciao,
                      Marc 'BlackJack' Rintsch

                      Comment

                      • Arnaud Delobelle

                        #12
                        Re: Setting an attribute without calling __setattr__()

                        aahz@pythoncraf t.com (Aahz) writes:
                        In article <m27ielb2i8.fsf @googlemail.com >,
                        Arnaud Delobelle <arnodel@google mail.comwrote:
                        >>Joshua Kugler <jkugler@bigfoo t.comwrites:
                        >>>
                        >> self.me = []
                        >> for v in obj:
                        >> self.me.append( ObjectProxy(v))
                        >>
                        >>Note that is could be spelt:
                        >>
                        >>self.me = map(ObjectProxy , v)
                        ^-- I meant obj!
                        >
                        It could also be spelt:
                        >
                        self.me = [ObjectProxy(v) for v in obj]
                        >
                        which is my preferred spelling....
                        I was waiting patiently for this reply... And your preferred spelling
                        is py3k-proof as well, of course.

                        I don't write map(lambda x: x+1, L) or map(itemgetter( 'x'), L) but I
                        like to use it when the first argument is a named function,
                        e.g. map(str, list_of_ints).

                        --
                        Arnaud

                        Comment

                        • Joshua Kugler

                          #13
                          Re: Setting an attribute without calling __setattr__()

                          animalMutha wrote:
                          >Consider reading the *second* paragraph about __setattr__ in section
                          >3.4.2 of the Python Reference Manual.
                          >
                          if you are simply going to answer rtfm - might as well kept it to
                          yourself.
                          For what it's worth, I (the original poster) am glad he answered that way.
                          It showed me the section and paragraph I had overlooked when reading
                          through the docs the first time.

                          j

                          Comment

                          Working...