I have a class with an __init__
function.
How can I return an integer value from this function when an object is created?
I wrote a program, where __init__
does command line parsing and I need to have some value set. Is it OK set it in global variable and use it in other member functions? If so how to do that? So far, I declared a variable outside class. and setting it one function doesn't reflect in other function ??
From the documentation of
__init__
:As a proof, this code:
Gives this error:
Why would you want to do that?
If you want to return some other object when a class is called, then use the
__new__()
method:__init__
doesn't return anything and should always returnNone
.__init__
is required to return None. You cannot (or at least shouldn't) return something else.Try making whatever you want to return an instance variable (or function).
You can just set it to a class variable and read it from the main program:
Well, if you don't care about the object instance anymore ... you can just replace it!