scared about refrences...

Discussion in 'Python' started by SpreadTooThin, Oct 30, 2006.

  1. I'm really worried that python may is doing some things I wasn't
    expecting... but lets see...

    if I pass a list to a function def fn(myList):

    and in that function I modify an element in the list, then does the
    callers list get modied as well.

    def fn(list):
    list[1] = 0

    myList = [1, 2, 3]
    print myList
    fn(myList)
    print myList

    >>> [1,2,3]
    >>> [1,0,3]


    How can I avoid this? In this case this is a really simplified example
    but the effects are the same...
    How do I specify or create deep copies of objects that may contain
    other objects that may contain other
    object that may contain other objects....
    SpreadTooThin, Oct 30, 2006
    #1
    1. Advertising

  2. In <>, SpreadTooThin
    wrote:

    > I'm really worried that python may is doing some things I wasn't
    > expecting... but lets see...


    Expect that Python never copies something if don't ask explicitly for a
    copy.

    > if I pass a list to a function def fn(myList):
    >
    > and in that function I modify an element in the list, then does the
    > callers list get modied as well.
    >
    > def fn(list):
    > list[1] = 0
    >
    > myList = [1, 2, 3]
    > print myList
    > fn(myList)
    > print myList
    >
    >>>> [1,2,3]
    >>>> [1,0,3]

    >
    > How can I avoid this? In this case this is a really simplified example
    > but the effects are the same...


    In this case:

    def fn(lst):
    lst = list(lst)
    lst[1] = 0


    > How do I specify or create deep copies of objects that may contain
    > other objects that may contain other object that may contain other
    > objects....


    See the `copy` module especially `copy.deepcopy()`.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Oct 30, 2006
    #2
    1. Advertising

  3. SpreadTooThin

    Neil Cerutti Guest

    On 2006-10-30, SpreadTooThin <> wrote:
    > def fn(list):
    > list[1] = 0
    >
    > myList = [1, 2, 3]
    > print myList
    > fn(myList)
    > print myList
    >
    >>>> [1,2,3]
    >>>> [1,0,3]

    >
    > How can I avoid this? In this case this is a really simplified
    > example but the effects are the same... How do I specify or
    > create deep copies of objects that may contain other objects
    > that may contain other object that may contain other
    > objects....


    See 3.18 Copy -- Shallow and deep copy operations.

    --
    Neil Cerutti
    I pulled into a lay-by with smoke coming from under the bonnet. I
    realized the car was on fire so took my dog and smothered it with a
    blanket. --Insurance Claim Blooper
    Neil Cerutti, Oct 30, 2006
    #3
  4. SpreadTooThin

    John Henry Guest

    I am no Python guru - just an ordinary user.

    There is nothing "scary" about this. There are (many) situations where
    this is actually *desirable* but of course there are (many) situations
    where this is an unwelcomed side-effect.

    In situations where I don't want this to happen, I simply pass down the
    list as an non-mutable object (like converting the list to a tuple).

    It took me a little bit of getting used to this concept as well:
    everything is either a mutable object, or a non-mutable object. I just
    have to throw away trying to use concept of "pointers" in Python.

    SpreadTooThin wrote:
    > I'm really worried that python may is doing some things I wasn't
    > expecting... but lets see...
    >
    > if I pass a list to a function def fn(myList):
    >
    > and in that function I modify an element in the list, then does the
    > callers list get modied as well.
    >
    > def fn(list):
    > list[1] = 0
    >
    > myList = [1, 2, 3]
    > print myList
    > fn(myList)
    > print myList
    >
    > >>> [1,2,3]
    > >>> [1,0,3]

    >
    > How can I avoid this? In this case this is a really simplified example
    > but the effects are the same...
    > How do I specify or create deep copies of objects that may contain
    > other objects that may contain other
    > object that may contain other objects....
    John Henry, Oct 30, 2006
    #4
  5. SpreadTooThin wrote:

    > I'm really worried that python may is doing some things I wasn't
    > expecting... but lets see...
    >
    > if I pass a list to a function def fn(myList):
    >
    > and in that function I modify an element in the list, then does the
    > callers list get modied as well.
    >
    > def fn(list):
    > list[1] = 0
    >
    > myList = [1, 2, 3]
    > print myList
    > fn(myList)
    > print myList
    >
    >>>> [1,2,3]
    >>>> [1,0,3]

    >
    > How can I avoid this?


    by not modifying your arguments nilly-willy, of course.

    > How do I specify or create deep copies of objects that may contain
    > other objects that may contain other
    > object that may contain other objects....


    http://www.effbot.org/pyfaq/how-do-i-copy-an-object-in-python.htm

    but if you find yourself needing to copy things a lot, you need to look
    over your design. well-designed Python code seldom needs to copy object
    values.

    </F>
    Fredrik Lundh, Oct 30, 2006
    #5
  6. Marc 'BlackJack' Rintsch wrote:
    > In <>, SpreadTooThin
    > wrote:
    >
    > > I'm really worried that python may is doing some things I wasn't
    > > expecting... but lets see...

    >
    > Expect that Python never copies something if don't ask explicitly for a
    > copy.
    >
    > > if I pass a list to a function def fn(myList):
    > >
    > > and in that function I modify an element in the list, then does the
    > > callers list get modied as well.
    > >
    > > def fn(list):
    > > list[1] = 0
    > >
    > > myList = [1, 2, 3]
    > > print myList
    > > fn(myList)
    > > print myList
    > >
    > >>>> [1,2,3]
    > >>>> [1,0,3]

    > >
    > > How can I avoid this? In this case this is a really simplified example
    > > but the effects are the same...

    >
    > In this case:
    >
    > def fn(lst):
    > lst = list(lst)
    > lst[1] = 0
    >
    >
    > > How do I specify or create deep copies of objects that may contain
    > > other objects that may contain other object that may contain other
    > > objects....

    >
    > See the `copy` module especially `copy.deepcopy()`.
    >

    I'm aware of __deepcopy__ but does that mean that every object in the
    object
    needs to have its own deep copy? And how do I ensure that?




    > Ciao,
    > Marc 'BlackJack' Rintsch
    SpreadTooThin, Oct 30, 2006
    #6
  7. Marc 'BlackJack' Rintsch wrote:
    > In <>, SpreadTooThin
    > wrote:
    >
    > > I'm really worried that python may is doing some things I wasn't
    > > expecting... but lets see...

    >
    > Expect that Python never copies something if don't ask explicitly for a
    > copy.
    >
    > > if I pass a list to a function def fn(myList):
    > >
    > > and in that function I modify an element in the list, then does the
    > > callers list get modied as well.
    > >
    > > def fn(list):
    > > list[1] = 0
    > >
    > > myList = [1, 2, 3]
    > > print myList
    > > fn(myList)
    > > print myList
    > >
    > >>>> [1,2,3]
    > >>>> [1,0,3]

    > >
    > > How can I avoid this? In this case this is a really simplified example
    > > but the effects are the same...

    >
    > In this case:
    >
    > def fn(lst):
    > lst = list(lst)
    > lst[1] = 0
    >
    >
    > > How do I specify or create deep copies of objects that may contain
    > > other objects that may contain other object that may contain other
    > > objects....

    >
    > See the `copy` module especially `copy.deepcopy()`.
    >


    This appears to be the right thing to do to me.. (but what do I know?)
    I tried this which more closely resembles my project but this doesn't
    work:

    import array
    import copy

    class test:
    def __init__(self):
    self.a = array.array('H', [1, 2, 3])
    self.b = ['a', 'b', 'c']

    def dump(self):
    print self.a, self.b

    t = test()
    t.dump()

    def testit(x):
    t = copy.deepcopy(x)
    t.a[1] = 0
    t.b[1] = 0

    testit(t)

    t.dump()
    SpreadTooThin, Oct 30, 2006
    #7
  8. On Mon, 30 Oct 2006 13:10:47 -0800, SpreadTooThin wrote:

    >> > How do I specify or create deep copies of objects that may contain
    >> > other objects that may contain other object that may contain other
    >> > objects....

    >>
    >> See the `copy` module especially `copy.deepcopy()`.
    >>

    >
    > This appears to be the right thing to do to me.. (but what do I know?)


    Yes, copy.deepcopy() is the thing you want.

    But remember Fredrik's advice that well-designed Python code should not
    need to copy data structures often. I don't think I've ever needed to use
    deepcopy, and rarely copy.copy().

    In general, functions should not modify their caller's data. So this is
    bad practice:

    def print_list(alist):
    """Print a sorted list"""
    alist.sort() # modifies the caller's data -- bad!
    for index, value in enumerate:
    print "Value %s at index %d" % (index, value)

    This is better:

    def print_list(alist):
    """Print a sorted list"""
    alist = alist[:] # makes a local shallow copy of the list
    alist.sort() # safe to modify now
    for index, value in enumerate:
    print "Value %s at index %d" % (index, value)

    But notice that you only need a shallow copy, not a deep copy, because you
    aren't modifying the objects within the list, only the list itself.



    > I tried this which more closely resembles my project but this doesn't
    > work:


    Unfortunately my crystal ball is back at the shop being repaired, so
    you'll have to explain what "doesn't work" means in this case. Does it
    raise an exception? If so, please post the exception. Does it do something
    different from what you expected? Then what did you expect, and what did
    it do?


    --
    Steven.
    Steven D'Aprano, Oct 30, 2006
    #8
  9. Steven D'Aprano wrote:
    > On Mon, 30 Oct 2006 13:10:47 -0800, SpreadTooThin wrote:
    >
    > >> > How do I specify or create deep copies of objects that may contain
    > >> > other objects that may contain other object that may contain other
    > >> > objects....
    > >>
    > >> See the `copy` module especially `copy.deepcopy()`.
    > >>

    > >
    > > This appears to be the right thing to do to me.. (but what do I know?)

    >
    > Yes, copy.deepcopy() is the thing you want.
    >
    > But remember Fredrik's advice that well-designed Python code should not
    > need to copy data structures often. I don't think I've ever needed to use
    > deepcopy, and rarely copy.copy().
    >
    > In general, functions should not modify their caller's data. So this is
    > bad practice:
    >
    > def print_list(alist):
    > """Print a sorted list"""
    > alist.sort() # modifies the caller's data -- bad!
    > for index, value in enumerate:
    > print "Value %s at index %d" % (index, value)
    >
    > This is better:
    >
    > def print_list(alist):
    > """Print a sorted list"""
    > alist = alist[:] # makes a local shallow copy of the list
    > alist.sort() # safe to modify now
    > for index, value in enumerate:
    > print "Value %s at index %d" % (index, value)
    >
    > But notice that you only need a shallow copy, not a deep copy, because you
    > aren't modifying the objects within the list, only the list itself.
    >
    >
    >
    > > I tried this which more closely resembles my project but this doesn't
    > > work:

    >
    > Unfortunately my crystal ball is back at the shop being repaired, so
    > you'll have to explain what "doesn't work" means in this case. Does it
    > raise an exception? If so, please post the exception. Does it do something
    > different from what you expected? Then what did you expect, and what did
    > it do?
    >

    I seems that some of the objects in the list don't get along well with
    deep copy..
    See my second example post that used deepcopy... When run blows up...





    >
    > --
    > Steven.
    SpreadTooThin, Oct 30, 2006
    #9
  10. SpreadTooThin wrote:
    > Steven D'Aprano wrote:
    >> On Mon, 30 Oct 2006 13:10:47 -0800, SpreadTooThin wrote:
    >>
    >>>>> How do I specify or create deep copies of objects that may contain
    >>>>> other objects that may contain other object that may contain other
    >>>>> objects....
    >>>> See the `copy` module especially `copy.deepcopy()`.
    >>>>
    >>> This appears to be the right thing to do to me.. (but what do I know?)

    >> Yes, copy.deepcopy() is the thing you want.
    >>
    >> But remember Fredrik's advice that well-designed Python code should not
    >> need to copy data structures often. I don't think I've ever needed to use
    >> deepcopy, and rarely copy.copy().
    >>
    >> In general, functions should not modify their caller's data. So this is
    >> bad practice:
    >>
    >> def print_list(alist):
    >> """Print a sorted list"""
    >> alist.sort() # modifies the caller's data -- bad!
    >> for index, value in enumerate:
    >> print "Value %s at index %d" % (index, value)
    >>
    >> This is better:
    >>
    >> def print_list(alist):
    >> """Print a sorted list"""
    >> alist = alist[:] # makes a local shallow copy of the list
    >> alist.sort() # safe to modify now
    >> for index, value in enumerate:
    >> print "Value %s at index %d" % (index, value)
    >>
    >> But notice that you only need a shallow copy, not a deep copy, because you
    >> aren't modifying the objects within the list, only the list itself.
    >>
    >>
    >>
    >>> I tried this which more closely resembles my project but this doesn't
    >>> work:

    >> Unfortunately my crystal ball is back at the shop being repaired, so
    >> you'll have to explain what "doesn't work" means in this case. Does it
    >> raise an exception? If so, please post the exception. Does it do something
    >> different from what you expected? Then what did you expect, and what did
    >> it do?
    >>

    > I seems that some of the objects in the list don't get along well with
    > deep copy..
    > See my second example post that used deepcopy... When run blows up...
    >

    When it blows up, is there a lot of shrapnel, or just smoke and fire?
    Is the shrapnel mostly metal, or is it plastic and glass?

    In short, if we don't know what's happening, we can't help.
    * Did the program spit out a bunch of text you didn't understand?
    If so, show us the text. That text may be incomprehensible at first,
    but it contains crucial clues.

    * Did it close your python window without a word?
    Tell us.

    * Did your computer freeze up?
    Tell us.

    If you don't tell us what went wrong *exactly*, you won't get a
    satisfactory answer.

    Cheers,
    Cliff
    J. Clifford Dyer, Oct 30, 2006
    #10
  11. I think you are afraid of references because you are not trusting your
    own code. You are afraid it will do "magic" behind the scenes and mess
    everything up. One way around that is to simply write better code and
    test often.

    If everything was copied when passed around it would be pretty awful --
    imagine passing around a 1Gb worth of data to a function...

    In Python all the primitives are copied and all other entities are
    references. If you want to just copy a list you can :
    1) use the list class: new_list=list(old_list)
    2) use the [:] syntax: new_list=old_list[:]
    3) if your list has nested lists, for example, the above methods will
    not copy everything, so you need a deep copy -- copy.deepcopy()

    Hope this helps,
    Nick V.


    SpreadTooThin wrote:
    > I'm really worried that python may is doing some things I wasn't
    > expecting... but lets see...
    >
    > if I pass a list to a function def fn(myList):
    >
    > and in that function I modify an element in the list, then does the
    > callers list get modied as well.
    >
    > def fn(list):
    > list[1] = 0
    >
    > myList = [1, 2, 3]
    > print myList
    > fn(myList)
    > print myList
    >
    > >>> [1,2,3]
    > >>> [1,0,3]

    >
    > How can I avoid this? In this case this is a really simplified example
    > but the effects are the same...
    > How do I specify or create deep copies of objects that may contain
    > other objects that may contain other
    > object that may contain other objects....
    Nick Vatamaniuc, Oct 30, 2006
    #11
  12. J. Clifford Dyer wrote:
    > SpreadTooThin wrote:
    > > Steven D'Aprano wrote:
    > >> On Mon, 30 Oct 2006 13:10:47 -0800, SpreadTooThin wrote:
    > >>
    > >>>>> How do I specify or create deep copies of objects that may contain
    > >>>>> other objects that may contain other object that may contain other
    > >>>>> objects....
    > >>>> See the `copy` module especially `copy.deepcopy()`.
    > >>>>
    > >>> This appears to be the right thing to do to me.. (but what do I know?)
    > >> Yes, copy.deepcopy() is the thing you want.
    > >>
    > >> But remember Fredrik's advice that well-designed Python code should not
    > >> need to copy data structures often. I don't think I've ever needed to use
    > >> deepcopy, and rarely copy.copy().
    > >>
    > >> In general, functions should not modify their caller's data. So this is
    > >> bad practice:
    > >>
    > >> def print_list(alist):
    > >> """Print a sorted list"""
    > >> alist.sort() # modifies the caller's data -- bad!
    > >> for index, value in enumerate:
    > >> print "Value %s at index %d" % (index, value)
    > >>
    > >> This is better:
    > >>
    > >> def print_list(alist):
    > >> """Print a sorted list"""
    > >> alist = alist[:] # makes a local shallow copy of the list
    > >> alist.sort() # safe to modify now
    > >> for index, value in enumerate:
    > >> print "Value %s at index %d" % (index, value)
    > >>
    > >> But notice that you only need a shallow copy, not a deep copy, because you
    > >> aren't modifying the objects within the list, only the list itself.
    > >>
    > >>
    > >>
    > >>> I tried this which more closely resembles my project but this doesn't
    > >>> work:
    > >> Unfortunately my crystal ball is back at the shop being repaired, so
    > >> you'll have to explain what "doesn't work" means in this case. Does it
    > >> raise an exception? If so, please post the exception. Does it do something
    > >> different from what you expected? Then what did you expect, and what did
    > >> it do?
    > >>

    > > I seems that some of the objects in the list don't get along well with
    > > deep copy..
    > > See my second example post that used deepcopy... When run blows up...
    > >

    > When it blows up, is there a lot of shrapnel, or just smoke and fire?
    > Is the shrapnel mostly metal, or is it plastic and glass?
    >
    > In short, if we don't know what's happening, we can't help.
    > * Did the program spit out a bunch of text you didn't understand?
    > If so, show us the text. That text may be incomprehensible at first,
    > but it contains crucial clues.
    >
    > * Did it close your python window without a word?
    > Tell us.
    >
    > * Did your computer freeze up?
    > Tell us.
    >
    > If you don't tell us what went wrong *exactly*, you won't get a
    > satisfactory answer.
    >


    I would assume that looking at the code you should be able to tell..
    Silly me.. Here.. is the log.. If I were helping.. I would have cut
    and pasted the code myself and ran it.. instead of trying to interpret
    this...

    array('H', [1, 2, 3]) ['a', 'b', 'c']
    Traceback (most recent call last):
    File
    "/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
    line 1806, in runMain
    self.dbg.runfile(debug_args[0], debug_args)
    File
    "/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
    line 1529, in runfile
    h_execfile(file, args, module=main, tracer=self)
    File
    "/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
    line 590, in __init__
    execfile(file, globals, locals)
    File "/Volumes/Data/Users/bjobrien/Desktop/pythonDICOM/Text-1.py",
    line 20, in __main__
    test(t)
    File "/Volumes/Data/Users/bjobrien/Desktop/pythonDICOM/Text-1.py",
    line 16, in test
    t = copy.deepcopy(x)
    File
    "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    line 174, in deepcopy
    y = copier(x, memo)
    File
    "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    line 305, in _deepcopy_inst
    state = deepcopy(state, memo)
    File
    "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    line 174, in deepcopy
    y = copier(x, memo)
    File
    "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    line 268, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
    File
    "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    line 185, in deepcopy
    y = copier(x, memo)
    TypeError: __deepcopy__() takes no arguments (1 given)



    > Cheers,
    > Cliff
    SpreadTooThin, Oct 31, 2006
    #12
  13. At Monday 30/10/2006 20:37, Nick Vatamaniuc wrote:

    >In Python all the primitives are copied and all other entities are
    >references.


    What do you mean?
    primitives==builtin classes?
    entities==objects?
    In Python, objects are never automatically copied, either builtin or
    user-defined. Even 123 is an object. Names provide references to objects.
    Perhaps you were thinking of *another* language.


    --
    Gabriel Genellina
    Softlab SRL

    __________________________________________________
    Correo Yahoo!
    Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
    Gabriel Genellina, Oct 31, 2006
    #13
  14. SpreadTooThin wrote:
    > J. Clifford Dyer wrote:
    >> SpreadTooThin wrote:
    >>> Steven D'Aprano wrote:
    >>>> On Mon, 30 Oct 2006 13:10:47 -0800, SpreadTooThin wrote:
    >>>>
    >>>>
    >>> I seems that some of the objects in the list don't get along well with
    >>> deep copy..
    >>> See my second example post that used deepcopy... When run blows up...
    >>>

    >> When it blows up, is there a lot of shrapnel, or just smoke and fire?
    >> Is the shrapnel mostly metal, or is it plastic and glass?
    >>
    >> In short, if we don't know what's happening, we can't help.
    >> * Did the program spit out a bunch of text you didn't understand?
    >> If so, show us the text. That text may be incomprehensible at first,
    >> but it contains crucial clues.
    >>
    >> * Did it close your python window without a word?
    >> Tell us.
    >>
    >> * Did your computer freeze up?
    >> Tell us.
    >>
    >> If you don't tell us what went wrong *exactly*, you won't get a
    >> satisfactory answer.
    >>

    >
    > I would assume that looking at the code you should be able to tell..
    > Silly me.. Here.. is the log.. If I were helping.. I would have cut
    > and pasted the code myself and ran it.. instead of trying to interpret
    > this...


    I know it seems unnecessary to post the traceback when I could get the
    same thing by running your code on my machine, but it's actually useful,
    for a couple reasons: First, when I run the code, I might not get an
    error, or if I do, it might not be the same error you were getting, and
    then we'd be on a wild goose chase. This could be because your python
    installation is goofy, or because you copied in your code incorrectly.
    Shit happens, and I'd rather not even start down one of those blind
    alleys. Third, it provides a useful frame for how to look at your
    code. While a traceback might look like a big mess at first, it's
    actually pretty easy to skim through once you get used to it, and it
    tells me where to focus my attention in your code.

    >
    > array('H', [1, 2, 3]) ['a', 'b', 'c']
    > Traceback (most recent call last):
    > File
    > "/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
    > line 1806, in runMain
    > self.dbg.runfile(debug_args[0], debug_args)
    > File
    > "/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
    > line 1529, in runfile
    > h_execfile(file, args, module=main, tracer=self)
    > File
    > "/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
    > line 590, in __init__
    > execfile(file, globals, locals)
    > File "/Volumes/Data/Users/bjobrien/Desktop/pythonDICOM/Text-1.py",
    > line 20, in __main__
    > test(t)
    > File "/Volumes/Data/Users/bjobrien/Desktop/pythonDICOM/Text-1.py",
    > line 16, in test
    > t = copy.deepcopy(x)
    > File
    > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    > line 174, in deepcopy
    > y = copier(x, memo)
    > File
    > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    > line 305, in _deepcopy_inst
    > state = deepcopy(state, memo)
    > File
    > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    > line 174, in deepcopy
    > y = copier(x, memo)
    > File
    > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    > line 268, in _deepcopy_dict
    > y[deepcopy(key, memo)] = deepcopy(value, memo)
    > File
    > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    > line 185, in deepcopy
    > y = copier(x, memo)
    > TypeError: __deepcopy__() takes no arguments (1 given)
    >
    >
    >
    >> Cheers,
    >> Cliff

    >


    Thanks, that's very helpful. Playing with your code a bit, I narrowed
    the problem down to the array.array() structure. Looking at
    help(array), there's a method defined called __deepcopy__, which, it
    seems, takes no arguments, while deepcopy is passing it one argument.
    Looks like a bug in the array module to me. Maybe others with more
    experience using array will have some deeper insight.


    Cheers,
    Cliff
    J. Clifford Dyer, Oct 31, 2006
    #14
  15. SpreadTooThin

    Peter Otten Guest

    J. Clifford Dyer wrote:

    > Thanks, that's very helpful.  Playing with your code a bit, I narrowed
    > the problem down to the array.array() structure.  Looking at
    > help(array), there's a method defined called __deepcopy__, which, it
    > seems, takes no arguments, while deepcopy is passing it one argument.
    > Looks like a bug in the array module to me.  Maybe others with more


    Fixed in subversion: http://www.python.org/sf/1545837
    Peter Otten, Oct 31, 2006
    #15
  16. J. Clifford Dyer wrote:
    > SpreadTooThin wrote:
    > > J. Clifford Dyer wrote:
    > >> SpreadTooThin wrote:
    > >>> Steven D'Aprano wrote:
    > >>>> On Mon, 30 Oct 2006 13:10:47 -0800, SpreadTooThin wrote:
    > >>>>
    > >>>>
    > >>> I seems that some of the objects in the list don't get along well with
    > >>> deep copy..
    > >>> See my second example post that used deepcopy... When run blows up...
    > >>>
    > >> When it blows up, is there a lot of shrapnel, or just smoke and fire?
    > >> Is the shrapnel mostly metal, or is it plastic and glass?
    > >>
    > >> In short, if we don't know what's happening, we can't help.
    > >> * Did the program spit out a bunch of text you didn't understand?
    > >> If so, show us the text. That text may be incomprehensible at first,
    > >> but it contains crucial clues.
    > >>
    > >> * Did it close your python window without a word?
    > >> Tell us.
    > >>
    > >> * Did your computer freeze up?
    > >> Tell us.
    > >>
    > >> If you don't tell us what went wrong *exactly*, you won't get a
    > >> satisfactory answer.
    > >>

    > >
    > > I would assume that looking at the code you should be able to tell..
    > > Silly me.. Here.. is the log.. If I were helping.. I would have cut
    > > and pasted the code myself and ran it.. instead of trying to interpret
    > > this...

    >
    > I know it seems unnecessary to post the traceback when I could get the
    > same thing by running your code on my machine, but it's actually useful,
    > for a couple reasons: First, when I run the code, I might not get an
    > error, or if I do, it might not be the same error you were getting, and
    > then we'd be on a wild goose chase. This could be because your python
    > installation is goofy, or because you copied in your code incorrectly.
    > Shit happens, and I'd rather not even start down one of those blind
    > alleys. Third, it provides a useful frame for how to look at your
    > code. While a traceback might look like a big mess at first, it's
    > actually pretty easy to skim through once you get used to it, and it
    > tells me where to focus my attention in your code.
    >
    > >
    > > array('H', [1, 2, 3]) ['a', 'b', 'c']
    > > Traceback (most recent call last):
    > > File
    > > "/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
    > > line 1806, in runMain
    > > self.dbg.runfile(debug_args[0], debug_args)
    > > File
    > > "/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
    > > line 1529, in runfile
    > > h_execfile(file, args, module=main, tracer=self)
    > > File
    > > "/Volumes/Data/Users/bjobrien/Applications/Komodo.app/Contents/SharedSupport/dbgp/pythonlib/dbgp/client.py",
    > > line 590, in __init__
    > > execfile(file, globals, locals)
    > > File "/Volumes/Data/Users/bjobrien/Desktop/pythonDICOM/Text-1.py",
    > > line 20, in __main__
    > > test(t)
    > > File "/Volumes/Data/Users/bjobrien/Desktop/pythonDICOM/Text-1.py",
    > > line 16, in test
    > > t = copy.deepcopy(x)
    > > File
    > > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    > > line 174, in deepcopy
    > > y = copier(x, memo)
    > > File
    > > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    > > line 305, in _deepcopy_inst
    > > state = deepcopy(state, memo)
    > > File
    > > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    > > line 174, in deepcopy
    > > y = copier(x, memo)
    > > File
    > > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    > > line 268, in _deepcopy_dict
    > > y[deepcopy(key, memo)] = deepcopy(value, memo)
    > > File
    > > "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy.py",
    > > line 185, in deepcopy
    > > y = copier(x, memo)
    > > TypeError: __deepcopy__() takes no arguments (1 given)
    > >
    > >
    > >
    > >> Cheers,
    > >> Cliff

    > >

    >
    > Thanks, that's very helpful. Playing with your code a bit, I narrowed
    > the problem down to the array.array() structure. Looking at
    > help(array), there's a method defined called __deepcopy__, which, it
    > seems, takes no arguments, while deepcopy is passing it one argument.
    > Looks like a bug in the array module to me. Maybe others with more
    > experience using array will have some deeper insight.
    >
    >


    I don't understand why python would insist that everything must be a
    refrence...
    It is of course helpful sometime but other times its not... and now
    I'm sorta out
    of luck...
    I don't know how to make this structure immutable... Pickle it? Seems
    very
    inefficient to me...
    Every time I pass a variable now I will worry that it will be changed
    by the function...
    I haven't worried about things like this since the very early days of
    BASIC....
    I don't know.. maybe I have more to learn.


    > Cheers,
    > Cliff
    SpreadTooThin, Oct 31, 2006
    #16
  17. SpreadTooThin

    Tim Chase Guest

    > I don't know how to make this structure immutable... Pickle
    > it? Seems very inefficient to me...


    Well, classes can be made mostly immutable by intercepting the
    attempts to write to it...something like

    class Foo(object):
    def __setattr__( self, name, val ):
    raise TypeError("I'm sorry, Dave. You can't do that.")

    It might also be feasible to do something like this with a
    decorator...?

    > Every time I pass a variable now I will worry that it will be
    > changed by the function... I haven't worried about things like
    > this since the very early days of BASIC.... I don't know..
    > maybe I have more to learn.


    Well, there are at least solutions to this paranoia that occur to me:

    -make a deep-copy of the object in question before calling the
    function, and then pass the copy to the function so that it can't
    alter the original

    -trust/read the functions' documentation. Most functions that
    alter the contents of their parameters are kind enough to note as
    much. If functions go bunging with your parameters without
    documenting it, the function's coder needs to be smacked.

    -tkc
    Tim Chase, Oct 31, 2006
    #17
  18. At Tuesday 31/10/2006 14:16, SpreadTooThin wrote:

    >I don't understand why python would insist that everything must be a
    >refrence...
    >It is of course helpful sometime but other times its not... and now
    >I'm sorta out
    >of luck...
    >I don't know how to make this structure immutable... Pickle it? Seems
    >very
    >inefficient to me...
    >Every time I pass a variable now I will worry that it will be changed
    >by the function...
    >I haven't worried about things like this since the very early days of
    >BASIC....
    >I don't know.. maybe I have more to learn.


    If you haven't read this yet, you should:
    <http://www.effbot.org/zone/python-objects.htm>


    --
    Gabriel Genellina
    Softlab SRL

    __________________________________________________
    Correo Yahoo!
    Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
    Gabriel Genellina, Oct 31, 2006
    #18
  19. SpreadTooThin wrote:

    > Every time I pass a variable now I will worry that it will be changed
    > by the function...


    why? who's writing those scary functions that you cannot trust? and
    what makes you think they won't abuse any immutable data you give them?

    </F>
    Fredrik Lundh, Oct 31, 2006
    #19
  20. SpreadTooThin

    Steve Holden Guest

    SpreadTooThin wrote:
    [...]
    > I don't understand why python would insist that everything must be a
    > refrence...


    We can tell that :)

    > It is of course helpful sometime but other times its not... and now
    > I'm sorta out
    > of luck...


    There are very good reasons for Python's namespace model. Sure it's
    different from some other languages, but it's actually been some
    people's preferred semantics for a very long time now (the
    recently-deceased Ralph Griswold, may he rest in peace, chose a very
    similar model for Icon.

    > I don't know how to make this structure immutable... Pickle it? Seems
    > very
    > inefficient to me...
    > Every time I pass a variable now I will worry that it will be changed
    > by the function...
    > I haven't worried about things like this since the very early days of
    > BASIC....
    > I don't know.. maybe I have more to learn.
    >

    You do. Firstly, learn to leave your paranoia outside your programming
    life. If a function or method makes undocumented changes to its mutable
    parameters then it needs changing (or its documentation does).

    Adequate testing should reveal such nonsenses before release.

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
    Steve Holden, Oct 31, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sonic_soul

    trouble adding project refrences

    sonic_soul, Aug 28, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    328
    sonic_soul
    Aug 28, 2003
  2. Josh
    Replies:
    0
    Views:
    350
  3. JustSomeGuy

    newbie question on refrences.

    JustSomeGuy, Sep 10, 2003, in forum: Java
    Replies:
    5
    Views:
    392
  4. Colin Northway

    JSP-Java, object refrences

    Colin Northway, Feb 18, 2004, in forum: Java
    Replies:
    1
    Views:
    1,697
    Erwin Moller
    Feb 18, 2004
  5. Lawrence Oluyede
    Replies:
    2
    Views:
    322
    Jarek Zgoda
    Aug 22, 2003
Loading...

Share This Page