copy object?

Discussion in 'Python' started by lallous, Sep 1, 2009.

  1. lallous

    lallous Guest

    Hello

    I am new to python and have some questions.

    How to copy objects using another method than this:

    class op:
    def __init__(self, op):
    for x in dir(op):
    if x[:2] == "__":
    continue
    setattr(self, x, getattr(op, x))

    o = op(src)

    I tried to copy with "o = copy.copy(src)" but as soon as "src" is
    gone, "o"'s attributes are not correct, and I cannot use copy.deepcopy
    () because of this error:
    TypeError: object.__new__(SwigPyObject) is not safe, use
    SwigPyObject.__new__()

    Can the previous for loop be simplified and replaced with a map() and
    a lambda function?

    Thanks.
    lallous, Sep 1, 2009
    #1
    1. Advertising

  2. lallous

    Terry Reedy Guest

    lallous wrote:
    > Hello
    >
    > I am new to python and have some questions.
    >
    > How to copy objects using another method than this:
    >
    > class op:
    > def __init__(self, op):


    What do you expect op to be? Certainly not the class 'op'.

    > for x in dir(op):
    > if x[:2] == "__":
    > continue
    > setattr(self, x, getattr(op, x))
    >
    > o = op(src)
    >
    > I tried to copy with "o = copy.copy(src)" but as soon as "src" is
    > gone, "o"'s attributes are not correct, and I cannot use copy.deepcopy
    > () because of this error:
    > TypeError: object.__new__(SwigPyObject) is not safe, use
    > SwigPyObject.__new__()
    >
    > Can the previous for loop be simplified and replaced with a map() and
    > a lambda function?


    If you want the instance to be a copy of another instance, the easier
    place to make the copy would be in the __new__ method, where you might
    be able to use copy.copy.

    tjr
    Terry Reedy, Sep 1, 2009
    #2
    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. Jazz
    Replies:
    3
    Views:
    748
    Kristian Bisgaard Lassen
    Oct 28, 2003
  2. VisionSet
    Replies:
    8
    Views:
    4,873
    Tris Orendorff
    Apr 29, 2004
  3. Alex
    Replies:
    2
    Views:
    1,200
  4. Replies:
    26
    Views:
    2,083
    Roland Pibinger
    Sep 1, 2006
  5. Rahul
    Replies:
    2
    Views:
    742
Loading...

Share This Page