is it a bug in Module copy or i am wrong??

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

    is it a bug in Module copy or i am wrong??

    python version 2.5 in module copy

    we all know that copy have two method: copy() and deepcopy().
    and the explain is
    - A shallow copy constructs a new compound object and then (to the
    extent possible) inserts *the same objects* into it that the
    original contains.

    - A deep copy constructs a new compound object and then, recursively,
    inserts *copies* into it of the objects found in the original.

    so i try a example:
    import copy

    class A:
    i = 1

    class B:
    a = A()


    b = B()

    x=copy.copy(b)

    y=copy.deepcopy (b)

    print id(x.a), id(b.a)

    print id(y.a), id(y.a)

    the result:
    14505264 14505264
    14505264 14505264

    So maybe i have a wrong understand to deep copy and shallow copy or
    it is a bug ?

    please help me!!



  • Chris Rebert

    #2
    Re: is it a bug in Module copy or i am wrong??

    On Thu, Nov 6, 2008 at 11:59 PM, yoma <yomatatata@gma il.comwrote:
    python version 2.5 in module copy
    >
    we all know that copy have two method: copy() and deepcopy().
    and the explain is
    - A shallow copy constructs a new compound object and then (to the
    extent possible) inserts *the same objects* into it that the
    original contains.
    >
    - A deep copy constructs a new compound object and then, recursively,
    inserts *copies* into it of the objects found in the original.
    >
    so i try a example:
    import copy
    >
    class A:
    i = 1
    >
    class B:
    a = A()
    Note that `a` is a class variable, not an instance variable. This ends
    up being important.
    >
    >
    b = B()
    >
    x=copy.copy(b)
    >
    y=copy.deepcopy (b)
    I believe these only copy the instance variables of `b`. They do NOT
    copy the class `B` (IMHO, copying B would be weird and unexpected
    behavior here anyway) or its constituent variables, such as `a`.
    >
    print id(x.a), id(b.a)
    >
    print id(y.a), id(y.a)
    >
    the result:
    14505264 14505264
    14505264 14505264
    Thus this makes sense. These all refer to B's variable `a`, which is a
    class variable and therefore not copied by copy() or deepcopy()-ing
    `b`, an *instance* of class B.
    The fact that you can access `a` through B instances does not mean
    that `a` "belongs" to any instance of B and is merely a result of how
    Python's object system works.

    Disclaimer: I am not a CPython dev and did not look at the `copy`
    module's sources.

    Cheers,
    Chris
    --
    Follow the path of the Iguana...

    >
    So maybe i have a wrong understand to deep copy and shallow copy or
    it is a bug ?
    >
    please help me!!
    >
    >
    >
    --

    >

    Comment

    • Marc 'BlackJack' Rintsch

      #3
      Re: is it a bug in Module copy or i am wrong??

      On Thu, 06 Nov 2008 23:59:51 -0800, yoma wrote:
      import copy
      >
      class A:
      i = 1
      >
      class B:
      a = A()
      >
      >
      b = B()
      >
      x=copy.copy(b)
      >
      y=copy.deepcopy (b)
      >
      print id(x.a), id(b.a)
      >
      print id(y.a), id(y.a)
      >
      the result:
      14505264 14505264
      14505264 14505264
      >
      So maybe i have a wrong understand to deep copy and shallow copy or it
      is a bug ?
      Additionally to Chris' explanation: You will get the same `id()` for
      every "copy" of a 1 in CPython. Numbers are immutable so the CPython
      implementation caches small integers as an optimization.

      Ciao,
      Marc 'BlackJack' Rintsch

      Comment

      • Terry Reedy

        #4
        Re: is it a bug in Module copy or i am wrong??

        yoma wrote:
        python version 2.5 in module copy
        >
        we all know that copy have two method: copy() and deepcopy().
        and the explain is
        - A shallow copy constructs a new compound object and then (to the
        extent possible) inserts *the same objects* into it that the
        original contains.
        >
        - A deep copy constructs a new compound object and then, recursively,
        inserts *copies* into it of the objects found in the original.
        Read a little further: This module does not copy types like module,
        method, stack trace, stack frame, file, socket, window, array, or any
        similar types. It does ``copy'' functions and classes (shallow and
        deeply), by returning the original object unchanged
        so i try a example:
        import copy
        >
        class A:
        i = 1
        >
        class B:
        a = A()
        >
        >
        b = B()
        The only attribute of b itself is .__class__ == B, which as the above
        says, is 'copied' by not being copied. So either shallow or deep copies
        of b will have .__class__ == the original B with its original instance
        of a.

        tjr

        Comment

        Working...