deepcopy alternative?

Discussion in 'Python' started by none, Jan 29, 2007.

  1. none

    none Guest

    I have a very complex data structure which is basically a class object
    containing (sometimes many) other class objects, function references,
    ints, floats, etc. The man for the copy module states pretty clearly
    that it will not copy methods or functions. I've looked around for a
    while (prob just using the wrong keywords) and haven't found a good
    solution. As a workaround I've been using cPickle, loads(dumps(obj))
    which is incredibly slow (~8 sec for a 1000 elem list).

    I believe the only thing stopping me from doing a deepcopy is the
    function references, but I'm not sure. If so is there any way to
    transform a string into a function reference(w/o eval or exec)?

    Example
    from copy import deepcopy
    a = ClassObj([ClassObj([3.2, 'str', funcRef]), 4, ClassObj[funcRef])
    b = deepcopy(a)
    TypeError: function() takes at least 2 arguments (0 given)

    All I want is a deepcopy of the list. Any suggestions would be appreciated.

    Jack Trades

    PS: If the answer to this is in __getstate__() or __setstate__() is
    there some reference to how these should be implemented besides the
    library reference?
     
    none, Jan 29, 2007
    #1
    1. Advertising

  2. > I believe the only thing stopping me from doing a deepcopy is the
    > function references, but I'm not sure. If so is there any way to
    > transform a string into a function reference(w/o eval or exec)?


    what's your python version?
    for me deepcopy(lambda:1) does not work in py2.4 but it works in py2.5
    (in py2.4 i tried to override __deepcopy__ but it had no effect)
     
    Szabolcs Nagy, Jan 30, 2007
    #2
    1. Advertising

  3. En Mon, 29 Jan 2007 20:17:11 -0300, <"none <"@bag.python.org> escribió:

    > I have a very complex data structure which is basically a class object
    > containing (sometimes many) other class objects,


    What are "class objects"? instances of a given class, or a class itself?

    > function references,
    > ints, floats, etc. The man for the copy module states pretty clearly
    > that it will not copy methods or functions. I've looked around for a
    > while (prob just using the wrong keywords) and haven't found a good
    > solution. As a workaround I've been using cPickle, loads(dumps(obj))
    > which is incredibly slow (~8 sec for a 1000 elem list).
    >
    > I believe the only thing stopping me from doing a deepcopy is the
    > function references, but I'm not sure. If so is there any way to
    > transform a string into a function reference(w/o eval or exec)?


    What do you mean by "function reference"? A module-level function works
    fine both with pickle and copy. Methods do not; but it's not common to
    take methods out of a class and put them inside a tuple, by example...

    >
    > Example
    > from copy import deepcopy
    > a = ClassObj([ClassObj([3.2, 'str', funcRef]), 4, ClassObj[funcRef])
    > b = deepcopy(a)
    > TypeError: function() takes at least 2 arguments (0 given)


    Please provide a *running* example. I've added some definitions, corrected
    some syntax errors, and this full example works for me:

    --- cut ---
    from copy import deepcopy

    class ClassObj(object):
    def __init__(self, args):
    self.data = args

    def funcRef(x):
    print x

    a = ClassObj([ClassObj([3.2, 'str', funcRef]), 4, ClassObj([funcRef])])
    b = deepcopy(a)
    print a is b
    a.data.append('hello')
    print len(a.data), len(b.data)
    --- cut ---

    > PS: If the answer to this is in __getstate__() or __setstate__() is
    > there some reference to how these should be implemented besides the
    > library reference?


    Until you provide more info on your problem it's hard to tell.


    --
    Gabriel Genellina
     
    Gabriel Genellina, Jan 30, 2007
    #3
  4. none

    none Guest

    Szabolcs Nagy wrote:
    >> I believe the only thing stopping me from doing a deepcopy is the
    >> function references, but I'm not sure. If so is there any way to
    >> transform a string into a function reference(w/o eval or exec)?

    >
    > what's your python version?
    > for me deepcopy(lambda:1) does not work in py2.4 but it works in py2.5
    > (in py2.4 i tried to override __deepcopy__ but it had no effect)
    >


    Thanks that fixed the problem real quick :)

    Jack Trades
     
    none, Jan 30, 2007
    #4
    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. Replies:
    0
    Views:
    628
  2. Eugeni Doljenko

    Legitimacy of deepcopy

    Eugeni Doljenko, Jun 4, 2004, in forum: Python
    Replies:
    2
    Views:
    324
    David Bolen
    Jun 7, 2004
  3. Donnal Walter

    deepcopy in new-style classes

    Donnal Walter, Jun 22, 2004, in forum: Python
    Replies:
    0
    Views:
    269
    Donnal Walter
    Jun 22, 2004
  4. OKB (not okblacke)

    deepcopy raises TypeError for method/function?

    OKB (not okblacke), Sep 9, 2004, in forum: Python
    Replies:
    3
    Views:
    398
    Alex Martelli
    Sep 10, 2004
  5. Dan Perl
    Replies:
    0
    Views:
    327
    Dan Perl
    Sep 14, 2004
Loading...

Share This Page