I am trying to copy all the methods and attributes from a class to an instance of another class. Unfortunately I am having issues with properties. Here’s an example:
class ToAdd(object):
@property
def foo(self):
return 'foo!'
class Base(object):
pass
b = Base()
for item, val in ToAdd.__dict__.iteritems():
if not item.startswith('__'):
setattr(b, item, val)
When calling b.foo I expect to get 'foo!', but instead it returns <property at 0x104a73d08>.
Note that this is akin to a mixin, but I want it to work on instances instead of classes.
There is probably a way to accomplish what you want with less “magic”, but it is possible to change an instance’s class…