An interesting python problem

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

    #1

    An interesting python problem

    Hi,
    Look at the follow command in python command line, See what's
    interesting?:)
    [color=blue][color=green][color=darkred]
    >>> class A:[/color][/color][/color]
    i = 0[color=blue][color=green][color=darkred]
    >>> a = A()
    >>> b = A()
    >>> a.i = 1
    >>> print a.i, b.i[/color][/color][/color]
    1 0

    ---------------------------------------
    [color=blue][color=green][color=darkred]
    >>> class A:[/color][/color][/color]
    arr = [][color=blue][color=green][color=darkred]
    >>> a = A()
    >>> b = A()
    >>> a[/color][/color][/color]
    <__main__.A instance at 0x00C96698>[color=blue][color=green][color=darkred]
    >>> b[/color][/color][/color]
    <__main__.A instance at 0x00CA0760>[color=blue][color=green][color=darkred]
    >>> A[/color][/color][/color]
    <class __main__.A at 0x00B314B0>[color=blue][color=green][color=darkred]
    >>> a.arr.append("h aha")
    >>> print a.arr , b.arr[/color][/color][/color]
    ['haha'] ['haha'][color=blue][color=green][color=darkred]
    >>> a.arr = ["xixi"]
    >>> print a.arr , b.arr[/color][/color][/color]
    ['xixi'] ['haha'][color=blue][color=green][color=darkred]
    >>> A.arr[/color][/color][/color]
    ['haha'][color=blue][color=green][color=darkred]
    >>> A.arr.append("x x")
    >>> A.arr[/color][/color][/color]
    ['haha', 'xx'][color=blue][color=green][color=darkred]
    >>> a.arr[/color][/color][/color]
    ['xixi'][color=blue][color=green][color=darkred]
    >>> b.arr[/color][/color][/color]
    ['haha', 'xx'][color=blue][color=green][color=darkred]
    >>> b.arr.pop()[/color][/color][/color]
    'xx'[color=blue][color=green][color=darkred]
    >>> b.arr[/color][/color][/color]
    ['haha'][color=blue][color=green][color=darkred]
    >>> A.arr[/color][/color][/color]
    ['haha']

    -------------------------------------
    [color=blue][color=green][color=darkred]
    >>> class X:[/color][/color][/color]
    def __init__(self):
    self.arr = [][color=blue][color=green][color=darkred]
    >>> m = X()
    >>> n = X()
    >>> m.arr.append("h aha")
    >>> print m.arr, n.arr[/color][/color][/color]
    ['haha'] []

  • bruno modulix

    #2
    Re: An interesting python problem

    Johnny Lee wrote:[color=blue]
    > Hi,
    > Look at the follow command in python command line, See what's
    > interesting?:)
    >
    >[color=green][color=darkred]
    >>>>class A:[/color][/color]
    >
    > i = 0
    >[color=green][color=darkred]
    >>>>a = A()
    >>>>b = A()
    >>>>a.i = 1
    >>>>print a.i, b.i[/color][/color]
    >
    > 1 0[/color]

    Quite what I would expect. First you declare i as being a *class*
    attribute of A, with value 0. Then you create 2 instances a and b of A.
    Then you add to a an *instance* variable named i (that then shadows the
    class variable of the same name), with value 1. Then you print a.i,
    wihci is the instance variable i of a, and b.i, which is the class
    variable i of A, with value 0.
    [color=blue]
    > ---------------------------------------
    >
    >[color=green][color=darkred]
    >>>>class A:[/color][/color]
    >
    > arr = []
    >[color=green][color=darkred]
    >>>>a = A()
    >>>>b = A()
    >>>>a[/color][/color]
    >
    > <__main__.A instance at 0x00C96698>
    >[color=green][color=darkred]
    >>>>b[/color][/color]
    >
    > <__main__.A instance at 0x00CA0760>
    >[color=green][color=darkred]
    >>>>A[/color][/color]
    >
    > <class __main__.A at 0x00B314B0>
    >[color=green][color=darkred]
    >>>>a.arr.appen d("haha")
    >>>>print a.arr , b.arr[/color][/color]
    >
    > ['haha'] ['haha'][/color]

    Now you create a class A with a *class* variable arr which is an empty
    list, and 2 instances a and b of A. Then you append to a.arr - which is
    A.arr, so when you print a.arr and b.arr, you in fact print A.arr
    [color=blue][color=green][color=darkred]
    >>>>a.arr = ["xixi"][/color][/color][/color]

    Then you add an instance variable arr to a, shadowing A.arr
    [color=blue][color=green][color=darkred]
    >>>>print a.arr , b.arr[/color][/color]
    >
    > ['xixi'] ['haha'][/color]

    So now you print a.arr and A.arr (accessed thru b)

    (snip)

    [color=blue]
    >[color=green][color=darkred]
    >>>>class X:[/color][/color]
    >
    > def __init__(self):
    > self.arr = []
    >[color=green][color=darkred]
    >>>>m = X()
    >>>>n = X()
    >>>>m.arr.appen d("haha")
    >>>>print m.arr, n.arr[/color][/color]
    >
    > ['haha'] []
    >[/color]

    Here you define a class X with an *instance* variable arr, and two
    instances m and n of X, then append to m.arr, which of course has no
    impact on n.

    I dont see anything interesting nor problematic here. If you understand
    the difference between class attributes and instance attributes, the
    difference between mutating an object and rebinding a name, and the
    attribute lookup rules in Python, you'll find that all this is the
    normal and expected behavior.

    Or did I miss something ?

    --
    bruno desthuilliers - is Python much more readable than Perl ???
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in 'onurb@xiludom. gro'.split('@')])"

    Comment

    • Johnny Lee

      #3
      Re: An interesting python problem


      bruno modulix wrote:[color=blue]
      >
      > I dont see anything interesting nor problematic here. If you understand
      > the difference between class attributes and instance attributes, the
      > difference between mutating an object and rebinding a name, and the
      > attribute lookup rules in Python, you'll find that all this is the
      > normal and expected behavior.
      >
      > Or did I miss something ?
      >[/color]

      No, you didn't miss anything as I can see. Thanks for your help:)

      Comment

      • bruno modulix

        #4
        Re: An interesting python problem

        Johnny Lee wrote:[color=blue]
        > bruno modulix wrote:
        >[color=green]
        >>I dont see anything interesting nor problematic here. If you understand
        >>the difference between class attributes and instance attributes, the
        >>difference between mutating an object and rebinding a name, and the
        >>attribute lookup rules in Python, you'll find that all this is the
        >>normal and expected behavior.
        >>
        >>Or did I miss something ?
        >>[/color]
        >
        >
        > No, you didn't miss anything as I can see. Thanks for your help:)[/color]

        You're welcome !-)

        Ok, I guess all this is not that intuitive, specially when comes from
        less dynamic languages. Python's object model is quite powerful, but one
        need to have a good understanding of it to understand *why* it works
        that way. There's an interesting slideshow about metaclasses and
        descriptors that may help (if your brain is robust enough !-) :



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

        Comment

        Working...