Question about None

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Jun 12 16:45:28 CEST 2009


Paul LaFollette a écrit :
> Kind people,
> 
> Using Python 3.0 on a Gatesware machine (XP).
> I am building a class in which I want to constrain the types that can
> be stored in various instance variables.

This somehow goes against the whole philosophy of dynamic typing Python 
is based upon... But there are indeed cases where it makes sense and I 
assume you know what you're doing !-)

>  For instance, I want to be
> certain that self.loc contains an int.  This is straightforward (as
> long as I maintain the discipline of changing loc through a method
> rather than just twiddling it directly.
> 
>   def setLoc(lo):
>     assert isinstance(lo, int), "loc must be an int"
>     self.loc = lo
> 
> does the trick nicely.

Did you considered using properties (or custom descriptors) instead ?

(snip - others already answered)



More information about the Python-list mailing list