probably at the beach
probably at the beach

Reputation: 15227

calling python __init__ on a class

I have a class (say called Account) saved a as a variable (say cur_class) and I want to initialize an instance of the class. I thought

cur_class.__init__() 

would work but it is giving me 'unbound method init() must be called with Account instance as first argument (got nothing instead)'. Obviously I'm doing something wrong - can anyone point me in the right direction?

Thanks, Richard

Upvotes: 0

Views: 195

Answers (1)

NPE
NPE

Reputation: 500883

Try cur_class(). For example:

In [1]: class C(object): pass
   ...: 

In [2]: cur_class = C

In [3]: obj = cur_class()

In [4]: obj
Out[4]: <__main__.C object at 0x1953c50>

A slightly longer explanation is that Python classes are callable. Calling a class returns a new instance.

Upvotes: 6

Related Questions