Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
I am having a class with an __init__ function.

How can I return the integer value from this function when an object is created?

I have written a program, where __init__ does command-line parsing and I need to have some value set.

Is it OK to set it in a 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 a function that doesn't reflect in other functions?

1 Answer

0 votes
by (36.8k points)

__init__ is required to return None. You cannot return something else.

Try using whatever you want to return an instance variable.

>>> class Foo:

...     def __init__(self):

...         return 42

... 

>>> foo = Foo()

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: __init__() should return None

 If you are a beginner and want to know more about Data Science the do check out the Data Science course

Related questions

Browse Categories

...