Take the 2-minute tour ×

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 ??

share|improve this question
thanks you all for your quick replies –  lakshmipathi Mar 22 '10 at 11:45
If you were considering returning an error code, raise an exception instead. –  Joe Gauterin Mar 22 '10 at 11:48
Please remove your comment and update your question. You own the question. It's your question. Please fix the question to correctly show what your real problem is. You're misusing __init__; we can help you if you describe what you're really trying to accomplish. –  S.Lott Mar 22 '10 at 14:47

6 Answers 6

up vote 29 down vote accepted

__init__ returns the newly created object. You cannot (or at least shouldn't) return something else.

Try making whatever you want to return an instance variable (or function).

>>> class Foo:
...     def __init__(self):
...         return 42
>>> foo = Foo()
Traceback (most recent call last):
TypeError: __init__() should return None
share|improve this answer
+1: You cannot return something else. It doesn't make any sense. –  S.Lott Mar 22 '10 at 14:45
init doesn't return the newly created object - as seen in the TypeError, it is required to return None, right? The newly created object is returned by new, init just sets some of its attributes. But yes, as you said, changing init, or new, to return something else really makes no sense. –  weronika Sep 26 '11 at 17:25

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:

class MyClass(object):
    def __init__(self):
        print "never called in this case"
    def __new__(cls):
        return 42

obj = MyClass()
print obj
share|improve this answer
Yeah, new is the right way of returning something other than a class instance when using a class... I'm just wondering - is there any reason you might actually want to DO that? –  weronika Sep 26 '11 at 17:28
@weronika One idea: in any situation where you'd normally use a factory, but you have some reason to want to present an interface that looks like normal class instantiation. Example: when adding some new optional parameters into your class's __init__, you realise that really, to provide the flexibility you want, you need a class factory that returns instances of specialised subclasses. But your library's users are already using your existing API. To preserve it, you override __new__ to return instances of your specialised subclasses. –  Mark Amery Jan 13 at 22:13
@MarkAmery That makes sense! –  weronika Jan 14 at 6:02
Done in Smalltalk and Obj-C all the time... See: NSArray –  AME Aug 1 at 8:15

From the documentation of __init__:


As a proof, this code:

class Foo(object):
    def __init__(self):
        return 2

f = Foo()

Gives this error:

Traceback (most recent call last):
  File "test_init.py", line 5, in <module>
    f = Foo()
TypeError: __init__() should return None, not 'int'
share|improve this answer

__init__ doesn't return anything and should always return None.

share|improve this answer

The __init__ method, like other methods and functions returns None by default in the absence of a return statement, so you can write it like either of these:

class Foo:
    def __init__(self):

class Bar:
    def __init__(self):
        return None

But, of course, adding the return None doesn't buy you anything.

I'm not sure what you are after, but you might be interested in one of these:

class Foo:
    def __init__(self):
    def __str__(self):
        return str(self.value)

print f.value
print f


share|improve this answer
ohh..Can i access member variable out the class ? Then that should solve my issue ....thanks –  lakshmipathi Mar 22 '10 at 12:06
@lakshmipathi, Yes, instance variables like this are public. –  quamrana Mar 22 '10 at 15:01

Sample Usage of the matter in question can be like:

class SampleObject(object)

    def __new__(cls,Item)
        if self.IsValid(Item):
            return super(SampleObject, cls).__new__(cls)
            return None

    def __init__(self,Item)
        self.InitData(Item) #large amount of data and very complex calculations


for i in data:
    if Item:             # in case the i data is valid for the sample object

I do not have enough reputation so I can not write a comment, it is crazy! I wish I could post it as a comment to weronika

share|improve this answer

Your Answer


