Static variable vs Class variable

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

    #31
    Re: Static variable vs Class variable

    Marc 'BlackJack' Rintsch <bj_666@gmx.net wrote:
    Simply not to introduce special cases I guess. If you write ``x.a +=
    b`` then `x.a` will be rebound whether an `a.__iadd__()` exists or
    not. Otherwise one would get interesting subtle differences with
    properties for example. If `x.a` is a property that checks if the
    value satisfies some constraints ``x.a += b`` would trigger the set
    method only if there is no `__iadd__()` involved if there's no
    rebinding.
    Unfortunately that doesn't work very well. If the validation rejects the
    new value the original is still modified:
    >>class C(object):
    def setx(self, value):
    if len(value)>2:
    raise ValueError
    self._x = value
    def getx(self):
    return self._x
    x = property(getx, setx)

    >>o = C()
    >>o.x = []
    >>o.x += ['a']
    >>o.x += ['b']
    >>o.x += ['c']
    Traceback (most recent call last):
    File "<pyshell#2 7>", line 1, in <module>
    o.x += ['c']
    File "<pyshell#2 2>", line 4, in setx
    raise ValueError
    ValueError
    >>o.x
    ['a', 'b', 'c']
    >>>

    Comment

    • Paul Melis

      #32
      Re: Static variable vs Class variable

      On Oct 17, 2:39 pm, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
      Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
      >
      Simply not to introduce special cases I guess. If you write ``x.a +=
      b`` then `x.a` will be rebound whether an `a.__iadd__()` exists or
      not. Otherwise one would get interesting subtle differences with
      properties for example. If `x.a` is a property that checks if the
      value satisfies some constraints ``x.a += b`` would trigger the set
      method only if there is no `__iadd__()` involved if there's no
      rebinding.
      >
      Unfortunately that doesn't work very well. If the validation rejects the
      new value the original is still modified:
      >
      >class C(object):
      >
      def setx(self, value):
      if len(value)>2:
      raise ValueError
      self._x = value
      def getx(self):
      return self._x
      x = property(getx, setx)
      >
      >o = C()
      >o.x = []
      >o.x += ['a']
      >o.x += ['b']
      >o.x += ['c']
      >
      Traceback (most recent call last):
      File "<pyshell#2 7>", line 1, in <module>
      o.x += ['c']
      File "<pyshell#2 2>", line 4, in setx
      raise ValueError
      ValueError
      >
      >o.x
      ['a', 'b', 'c']
      Now that's really interesting. I added a print "before" and print
      "after" statement just before and after the self._x = value and these
      *do not get called* after the exception is raised when the third
      element is added.

      Comment

      • Marc 'BlackJack' Rintsch

        #33
        Re: Static variable vs Class variable

        On Wed, 17 Oct 2007 05:57:50 -0700, Paul Melis wrote:
        On Oct 17, 2:39 pm, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
        >>class C(object):
        >>
        > def setx(self, value):
        > if len(value)>2:
        > raise ValueError
        > self._x = value
        > def getx(self):
        > return self._x
        > x = property(getx, setx)
        >>
        >>o = C()
        >>o.x = []
        >>o.x += ['a']
        >>o.x += ['b']
        >>o.x += ['c']
        >>
        >Traceback (most recent call last):
        > File "<pyshell#2 7>", line 1, in <module>
        > o.x += ['c']
        > File "<pyshell#2 2>", line 4, in setx
        > raise ValueError
        >ValueError
        >>
        >>o.x
        >['a', 'b', 'c']
        >
        Now that's really interesting. I added a print "before" and print
        "after" statement just before and after the self._x = value and these
        *do not get called* after the exception is raised when the third
        element is added.
        Well, of course not. Did you really expect that!? Why?

        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • Paul Melis

          #34
          Re: Static variable vs Class variable

          On Oct 17, 3:20 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
          On Wed, 17 Oct 2007 05:57:50 -0700, Paul Melis wrote:
          On Oct 17, 2:39 pm, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
          >class C(object):
          >
          def setx(self, value):
          if len(value)>2:
          raise ValueError
          self._x = value
          def getx(self):
          return self._x
          x = property(getx, setx)
          >
          >o = C()
          >o.x = []
          >o.x += ['a']
          >o.x += ['b']
          >o.x += ['c']
          >
          Traceback (most recent call last):
          File "<pyshell#2 7>", line 1, in <module>
          o.x += ['c']
          File "<pyshell#2 2>", line 4, in setx
          raise ValueError
          ValueError
          >
          >o.x
          ['a', 'b', 'c']
          >
          Now that's really interesting. I added a print "before" and print
          "after" statement just before and after the self._x = value and these
          *do not get called* after the exception is raised when the third
          element is added.
          >
          Well, of course not. Did you really expect that!? Why?
          Because o.x *is* updated, i.e. the net result of self._x = value is
          executed.

          Paul

          Comment

          • Paul Melis

            #35
            Re: Static variable vs Class variable

            On Oct 17, 3:41 pm, Paul Melis <paul.me...@gma il.comwrote:
            On Oct 17, 3:20 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
            >
            >
            >
            On Wed, 17 Oct 2007 05:57:50 -0700, Paul Melis wrote:
            On Oct 17, 2:39 pm, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
            >>class C(object):
            >
            > def setx(self, value):
            > if len(value)>2:
            > raise ValueError
            > self._x = value
            > def getx(self):
            > return self._x
            > x = property(getx, setx)
            >
            >>o = C()
            >>o.x = []
            >>o.x += ['a']
            >>o.x += ['b']
            >>o.x += ['c']
            >
            >Traceback (most recent call last):
            > File "<pyshell#2 7>", line 1, in <module>
            > o.x += ['c']
            > File "<pyshell#2 2>", line 4, in setx
            > raise ValueError
            >ValueError
            >
            >>o.x
            >['a', 'b', 'c']
            >
            Now that's really interesting. I added a print "before" and print
            "after" statement just before and after the self._x = value and these
            *do not get called* after the exception is raised when the third
            element is added.
            >
            Well, of course not. Did you really expect that!? Why?
            >
            Because o.x *is* updated, i.e. the net result of self._x = value is
            executed.
            Argh, the getter is of course used to append the third element, after
            which the rebinding triggers the exception. Got it now...

            Paul

            Comment

            • Hrvoje Niksic

              #36
              Re: Static variable vs Class variable

              Marc 'BlackJack' Rintsch <bj_666@gmx.net writes:
              Simply not to introduce special cases I guess. If you write ``x.a
              += b`` then `x.a` will be rebound whether an `a.__iadd__()` exists
              or not. Otherwise one would get interesting subtle differences with
              properties for example. If `x.a` is a property that checks if the
              value satisfies some constraints ``x.a += b`` would trigger the set
              method only if there is no `__iadd__()` involved if there's no
              rebinding.
              Note that such constraint is easily invalidated simply with:

              foo = x.a
              foo += b

              Comment

              • Hrvoje Niksic

                #37
                Re: Static variable vs Class variable

                Duncan Booth <duncan.booth@i nvalid.invalidw rites:
                Hrvoje Niksic <hniksic@xemacs .orgwrote:
                >
                >The current implementation of += uses __add__ for addition and
                >__iadd__ for addition that may or may not be in-place. I'd like to
                >know the rationale for that design.
                >>
                >
                Apart from the obvious short answer of being consistent (so you don't
                have to guess whether or not a+=b is going to do an assignment), I think
                the decision was partly to keep the implementation clean.
                The implementation, by necessity, supports a lot of historical cruft,
                so keeping it clean was not a priority. (This is not a criticism, I
                like how they kept backward compatibility.)
                Right now an inplace operation follows one of three patterns:
                [...]

                Thanks for pointing it out; I didn't realize just how much work went
                into the new assignment opcodes. Given the complexity, it's a wonder
                they're there at all!

                Comment

                • bambam

                  #38
                  Re: Static variable vs Class variable



                  "Steven D'Aprano" <steve@REMOVE-THIS-cybersource.com .auwrote in message
                  news:13hbvu6oc7 42l02@corp.supe rnews.com...
                  On Wed, 17 Oct 2007 13:41:06 +0200, Hrvoje Niksic wrote:
                  >
                  >The current implementation of += uses __add__ for addition and __iadd__
                  >for addition that may or may not be in-place. I'd like to know the
                  >rationale for that design.
                  >
                  Everything you need is in the PEP:
                  >
                  This PEP describes the augmented assignment proposal for Python 2.0. This PEP tracks the status and ownership of this feature, slated for introduction in Python 2.0. It contains a description of the feature and outlines changes necessary to support th...

                  >
                  >
                  >
                  --
                  Steven.
                  Which illustrates that the proposal, while simplified for implementation,
                  is not exactly what was desired*

                  """ is both more readable and less error prone, because it is
                  instantly obvious to the reader that it is <xthat is being
                  changed, and not <xthat is being replaced
                  """

                  As we see from this thread, it is not instantly obvious to the reader;
                  the meaning of "changed, not replaced" is ambiguous.

                  [david]

                  * That is, unless ambiguity was the desideratum


                  Comment

                  Working...