On 2/7/06, Michael Bayer <[EMAIL PROTECTED]> wrote:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66507
Have you seen this? This recipe suggests defining __copy__ to custimize the copy.copy process if you like to use copy. Says the __dict__.update could also work I think.
hey list -
I am doing a much needed reorganization with regards to the construction
and retreival of mappers to be simpler and more straightforward. one
issue I always run into with Python is that the copy.copy () method is
*super* slow...its operation is 10 to 30 (!!!) times slower than just
constructing a new object or using __new__ with a dictionary copy:
import copy, time
# test object
class foo(object):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
# create 5000 test objects
l = []
for x in range(0, 5000):
l.append(foo('a', 'b', 'c'))
# test harness
def test(copier, method):
x = []
now = time.time()
for f in l:
x.append(copier(f))
print method, ":", (time.time() - now)
# run three tests ten times
for g in range(0,10):
# test straight construction
test(lambda x: foo(x.a, x.b, x.c), "constructor")
# test copy function
test(lambda x: copy.copy(x), "copy")
# test using __new__ + dict copy
def makenew(x):
g = foo.__new__(foo)
g.__dict__.update(x.__dict__)
return g
test(makenew, "__new__")
So what is the best way to make a shallow (new-style-class) object copy?
is this approach:
y = klass.__new__(klass)
y.__dict__.update(x.__dict__)
which seems to work great, safe/compatible/complete ?
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66507
Have you seen this? This recipe suggests defining __copy__ to custimize the copy.copy process if you like to use copy. Says the __dict__.update could also work I think.
- mike
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users
--
---------------------------------------------------------------------------------------------------
John S. Yang

