I wrote a function in Python:
def instantiate(c):
if inspect.isclass(c): return c()
elif isinstance(c, object): return c
else: raise Exception, '%s is not an object or class.' % c
Now I want to do the opposite: get the class from an already instantiated object so that I can re-instantiate it with different parameters. How can I do that?
Tests:
>>> f = Form()
>>> type(f)()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: instance() takes at least 1 argument (0 given)
>>> f.__class__()
<forms.Form instance at 0xb7f4d5cc>
More tests:
>>> o = object()
>>> type(o)()
<object object at 0xb7f78478>
>>> o.__class__()
<object object at 0xb7f78480>
Seems to work for object
but not my Form
class:
class Form:
def __init__(self, data={}, prefix='', action='', id=None):
I'm guessing this has something to do with self
but I don't know what.