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
asked May 25, 2011 at 14:08
probably at the beach
15.3k17 gold badges82 silver badges123 bronze badges
1 Answer 1
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.
answered May 25, 2011 at 14:10
NPE
503k114 gold badges970 silver badges1k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
our_class().CurClass, as Python naming convention says thatcur_class(lower case with underscores) should be used for variable names.