The only cost here is the cost of looking up the method. Once we've got the method dispatch will be the same whether it's defined in the class or the instance. (There may be some improvements we can make to make methods in the class faster in the future, but this is true today).
In the case of old-style classes, it makes no difference (we'll lookup in the instance first, so presumably it could be even a little faster than having it on the class).
In the case of new-style classes, which can live w/o an instance dictionary, the cost will be paid when you add the first method. It's the same cost as if you had added some arbitrary field though, and that cost will be paid on every method call (as we'll be forced to check if the instance has overridden any methods). The cost here is that you're forcing an additional dictionary lookup on invocation. But we always check the instance first, so for you this means your path becomes fast, and the normal path becomes slow.
There's the copy module but we don't fully support __reduce__ everywhere you'd need it.
You can always copy the items in the dict, w/ any filter you want:
class foo(object):
def __init__(self):
self.bar = bar
def bar(self, xyz): pass
a = foo()
a.bar = bar
b = foo()
import operator
for x in a.__dict__.keys():
if operator.isCallable(a.__dict__[x]) and not b.__dict__.has_key(x):
b.__dict__[x] = a.__dict__[x]
Do you want to help develop Dynamic languages on CLR?<http://members.microsoft.com/careers/search/details.aspx?JobID=6D4754DE-11F0-45DF-8B78-DC1B43134038> (http://members.microsoft.com/careers/search/details.aspx?JobID=6D4754DE-11F0-45DF-8B78-DC1B43134038)
________________________________
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Latta
Sent: Sunday, April 23, 2006 10:44 AM
To: 'Discussion of IronPython'
Subject: [IronPython] Basic performance and feature questions
I have some questions about IronPython regarding some of the more dynamic features:
1) What is the relative performance cost of using methods added to an object rather than defined in a class?
2) Is there a way to clone an object that includes all the methods added to it?
In other words how well would IronPython support a Self type programming model where instances were the focus rather than classes?
Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/users-ironpython.com/attachments/20060424/e3dbec5e/attachment-0001.htm