Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   copy object? (http://www.velocityreviews.com/forums/t696746-copy-object.html)

lallous 09-01-2009 02:04 PM

copy object?
 
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.

Terry Reedy 09-01-2009 07:21 PM

Re: copy object?
 
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



All times are GMT. The time now is 12:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57