Subclassing int

Alex Popescu nospam.themindstorm at gmail.com
Tue Jul 24 10:24:05 CEST 2007


G <gggg.iiiii at gmail.com> wrote in
news:4a7f84ac0707231803x7083a688kc91965a5d78e679 at mail.gmail.com: 

> ------=_Part_187401_13883248.1185238999144
> Hi,
> 
>     I am trying to subclass int to allow a constructor to accept None.
>     I am 
> trying the following
> 
> class INT(int):
>     def __init__(self, x):
>         if x is  None:
>             return None
>         else:
>             int.__init__(x)
> 
> b = INT(x=None)
> 
> When i run the previous code i get the following error:
>  b = INT(x=None)
> TypeError: int() argument must be a string or a number, not
> 'NoneType'. 
> 
> Do you guys know why the if statement is not evaluated?
> 
> Thanks for your help
> 

Afaik __init__ must not return something. It is __new__ where you can do 
these kind of tricks.

bests,
./alex
--
.w( the_mindstorm )p.




More information about the Python-list mailing list