I have a class, whose __init__ function does a bunch of stuff, and that’s all I need.
So I really just want to call the __init__ function of this class. How can I do this, without assigning it to a variable?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First of all, this is clearly a design error – that code should just be a standalone function.
However, there are a couple of ways that you can do this:
Just instantiate as normal – there is no reason you have to assign the result of an expression to a variable. However, this will result in full object construction (and probably destruction shortly thereafter).
Call the underlying function:
This won’t trigger object construction, but you probably don’t want that. Note that in the example here, I’ve passed
Noneas the value ofself. This only works if__init__does nothing withself(or, handles the case where it isNone).2a. You could use staticmethod to make this easier: