[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Can't define __call__ within __init__?

Mark Lawrence

3/10/2010 1:40:00 PM

Neal Becker wrote:
> Want to switch __call__ behavior. Why doesn't this work? What is the
> correct way to write this?
>
> class X (object):
> def __init__(self, i):
> if i == 0:
> def __call__ (self):
> return 0
> else:
> def __call_ (self):
> return 1
>
>
> x = X(0)
>
> x()
> TypeError: 'X' object is not callable
>
>
I think it's because X has already been constructed when __init__ is
called, so __new__ is the place for your conditional code.