What a curious assignment.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neil.Jin@gmail.com

    #1

    What a curious assignment.

    [test 1][color=blue][color=green][color=darkred]
    >>> class A:[/color][/color][/color]
    .... i = 1
    ....[color=blue][color=green][color=darkred]
    >>> a = A()
    >>> A.i[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> a.i[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> A.i = 2
    >>> A.i[/color][/color][/color]
    2[color=blue][color=green][color=darkred]
    >>> a.i[/color][/color][/color]
    2[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    [test2][color=blue][color=green][color=darkred]
    >>> class A:[/color][/color][/color]
    .... i = 1
    ....[color=blue][color=green][color=darkred]
    >>> a = A()
    >>> A.i[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> a.i[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> a.i = 2
    >>> A.i[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> a.i[/color][/color][/color]
    2[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Is there somthing wrong????

  • Mike Meyer

    #2
    Re: What a curious assignment.

    "Neil.Jin@gmail .com" <Neil.Jin@gmail .com> writes:
    [color=blue]
    > [test 1][color=green][color=darkred]
    >>>> class A:[/color][/color]
    > ... i = 1
    > ...[color=green][color=darkred]
    >>>> a = A()
    >>>> A.i[/color][/color]
    > 1[color=green][color=darkred]
    >>>> a.i[/color][/color]
    > 1[color=green][color=darkred]
    >>>> A.i = 2
    >>>> A.i[/color][/color]
    > 2[color=green][color=darkred]
    >>>> a.i[/color][/color]
    > 2[color=green][color=darkred]
    >>>>[/color][/color]
    >
    > [test2][color=green][color=darkred]
    >>>> class A:[/color][/color]
    > ... i = 1
    > ...[color=green][color=darkred]
    >>>> a = A()
    >>>> A.i[/color][/color]
    > 1[color=green][color=darkred]
    >>>> a.i[/color][/color]
    > 1[color=green][color=darkred]
    >>>> a.i = 2
    >>>> A.i[/color][/color]
    > 1[color=green][color=darkred]
    >>>> a.i[/color][/color]
    > 2[color=green][color=darkred]
    >>>>[/color][/color]
    >
    > Is there somthing wrong????[/color]

    No. Reading a.i looks up i by checking the instance (a), then the
    class (A), so a.i and A.i are the same thing. So changing A.i changes
    the value seen by a.i. Binding a.i binds i to a, not A, so after the
    binding, a.i and A.i are different things.

    <mike
    --
    Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

    Comment

    • bonono@gmail.com

      #3
      Re: What a curious assignment.


      Neil.Jin@gmail. com wrote:[color=blue]
      > [test 1][color=green][color=darkred]
      > >>> class A:[/color][/color]
      > ... i = 1
      > ...[color=green][color=darkred]
      > >>> a = A()
      > >>> A.i[/color][/color]
      > 1[color=green][color=darkred]
      > >>> a.i[/color][/color]
      > 1[color=green][color=darkred]
      > >>> A.i = 2
      > >>> A.i[/color][/color]
      > 2[color=green][color=darkred]
      > >>> a.i[/color][/color]
      > 2[color=green][color=darkred]
      > >>>[/color][/color]
      >
      > [test2][color=green][color=darkred]
      > >>> class A:[/color][/color]
      > ... i = 1
      > ...[color=green][color=darkred]
      > >>> a = A()
      > >>> A.i[/color][/color]
      > 1[color=green][color=darkred]
      > >>> a.i[/color][/color]
      > 1[color=green][color=darkred]
      > >>> a.i = 2
      > >>> A.i[/color][/color]
      > 1[color=green][color=darkred]
      > >>> a.i[/color][/color]
      > 2[color=green][color=darkred]
      > >>>[/color][/color]
      >
      > Is there somthing wrong????[/color]
      I don't think so, the name binding stuff again. Good to know though.

      Comment

      • Naveed

        #4
        Re: What a curious assignment.

        "A.i" is a class attribute. "a.i" at first is the same as "A.i". Once
        you set a.i = 2, you are actually creating a new data attribute called
        i for the instance a. This happens on the fly. So then when you
        reference a.i, it uses the instance data attribute, instead of the
        class attribute.

        This might make it more clear. Try:
        a.f = 3
        print a.f
        Even though f is not declared in your class definition, the above code
        still prints 3. Because it created the data attribute f on the fly.

        Comment

        • Steven D'Aprano

          #5
          Re: What a curious assignment.

          Neil.Jin@gmail. com wrote:
          [color=blue]
          > Is there somthing wrong????[/color]

          Kids today, don't they learn about inheritence? :-)

          Python's object model is that instances inherit both
          methods and attributes from the class (and
          superclasses). Methods are just a special case of
          attributes: the method is a callable attribute.

          When you reference an attribute, Python first checks
          the instance by looking up instance.__dict __, and if
          that fails, it looks up instance.__clas s__.__dict__.

          (This is a simplification, e.g. it isn't exactly true
          for objects with slots.)

          For attribute lookup (that is, the attribute reference
          is on the right hand side of an assignment), the lookup
          may fail and so the class attribute may be retrieved.
          This is by design.

          For attribute assignment (that is, the attribute
          reference is on the left hand side of an assignment),
          the assignment will never fail.

          (Again, ignoring slots and any other special cases I
          have't thought of.)


          --
          Steven.

          Comment

          • bonono@gmail.com

            #6
            Re: What a curious assignment.


            Steven D'Aprano wrote:[color=blue]
            > Neil.Jin@gmail. com wrote:
            >[color=green]
            > > Is there somthing wrong????[/color]
            >
            > Kids today, don't they learn about inheritence? :-)
            >
            > Python's object model is that instances inherit both
            > methods and attributes from the class (and
            > superclasses). Methods are just a special case of
            > attributes: the method is a callable attribute.
            >
            > When you reference an attribute, Python first checks
            > the instance by looking up instance.__dict __, and if
            > that fails, it looks up instance.__clas s__.__dict__.
            >
            > (This is a simplification, e.g. it isn't exactly true
            > for objects with slots.)
            >
            > For attribute lookup (that is, the attribute reference
            > is on the right hand side of an assignment), the lookup
            > may fail and so the class attribute may be retrieved.
            > This is by design.
            >
            > For attribute assignment (that is, the attribute
            > reference is on the left hand side of an assignment),
            > the assignment will never fail.
            >
            > (Again, ignoring slots and any other special cases I
            > have't thought of.)
            >[/color]
            I believe he knows about inheritance, but not about the behaviour of
            the assignment. In many other OO languages, I believe you cannot have
            the same name for both instance variable and class variable. javascript
            has similar behaviour.

            Comment

            • bruno at modulix

              #7
              Re: What a curious assignment.

              Neil.Jin@gmail. com wrote:[color=blue]
              > [test 1]
              >[color=green][color=darkred]
              >>>>class A:[/color][/color]
              >
              > ... i = 1
              > ...
              >[color=green][color=darkred]
              >>>>a = A()
              >>>>A.i[/color][/color]
              >
              > 1
              >[color=green][color=darkred]
              >>>>a.i[/color][/color]
              >
              > 1
              >[color=green][color=darkred]
              >>>>A.i = 2
              >>>>A.i[/color][/color]
              >
              > 2
              >[color=green][color=darkred]
              >>>>a.i[/color][/color]
              >
              > 2
              >
              >
              > [test2]
              >[color=green][color=darkred]
              >>>>class A:[/color][/color]
              >
              > ... i = 1
              > ...
              >[color=green][color=darkred]
              >>>>a = A()
              >>>>A.i[/color][/color]
              >
              > 1
              >[color=green][color=darkred]
              >>>>a.i[/color][/color]
              >
              > 1
              >[color=green][color=darkred]
              >>>>a.i = 2
              >>>>A.i[/color][/color]
              >
              > 1
              >[color=green][color=darkred]
              >>>>a.i[/color][/color]
              >
              > 2
              >
              >
              > Is there somthing wrong????[/color]

              No.

              --
              bruno desthuilliers
              python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
              p in 'onurb@xiludom. gro'.split('@')])"

              Comment

              • Mike Meyer

                #8
                Re: What a curious assignment.

                "bonono@gmail.c om" <bonono@gmail.c om> writes:[color=blue]
                > I believe he knows about inheritance, but not about the behaviour of
                > the assignment. In many other OO languages, I believe you cannot have
                > the same name for both instance variable and class variable. javascript
                > has similar behaviour.[/color]

                I think more important is that in many languages you can't dynamically
                add attributes to an object. So an attempt to bind a.i will either
                fail, or be an assignment to A.i.

                <mike
                --
                Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                Comment

                • Steven D'Aprano

                  #9
                  Re: What a curious assignment.

                  On Wed, 23 Nov 2005 00:17:32 -0800, bonono@gmail.co m wrote:
                  [color=blue]
                  >
                  > Steven D'Aprano wrote:[color=green]
                  >> Neil.Jin@gmail. com wrote:
                  >>[color=darkred]
                  >> > Is there somthing wrong????[/color]
                  >>
                  >> Kids today, don't they learn about inheritence? :-)[/color][/color]

                  [snip]
                  [color=blue]
                  > I believe he knows about inheritance,[/color]

                  Hence my smiley.
                  [color=blue]
                  > but not about the behaviour of
                  > the assignment.[/color]

                  Which he now knows, based on trying it and seeing what happens.

                  [color=blue]
                  > In many other OO languages, I believe you cannot have
                  > the same name for both instance variable and class variable. javascript
                  > has similar behaviour.[/color]

                  I don't believe that is the problem. If that were the problem, the
                  original poster wouldn't have even tried to assign to the instance and the
                  class separately, surely.


                  --
                  Steven.

                  Comment

                  Working...