[Tutor] Using __init__ to return a value

vishwajeet singh dextrous85 at gmail.com
Wed Jun 12 11:47:26 CEST 2013


On Wed, Jun 12, 2013 at 3:02 PM, Khalid Al-Ghamdi <emailkgnow at gmail.com>wrote:

> Hi,
>
> Why doesn't this work? And is there way to have an
> object immediately return a value or object once it is instantiated with
> using a method call?
>

__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).

Refer this thread
http://stackoverflow.com/questions/2491819/python-init-how-to-return-a-value


>
>    1. >>> class k:
>    2.         def __init__(self,n):
>    3.                 return n*n
>    4.
>    5.
>    6. >>> khalid=k(3)
>    7. Traceback (most recent call last):
>    8.   File "<pyshell#58>", line 1, in <module>
>    9.     khalid=k(3)
>    10. TypeError: __init__() should return None, not 'int'
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Vishwajeet Singh
+91-9657702154 | dextrous85 at gmail.com | http://bootstraptoday.com
Twitter: http://twitter.com/vishwajeets | LinkedIn:
http://www.linkedin.com/in/singhvishwajeet
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130612/b0647eea/attachment.html>


More information about the Tutor mailing list