3 number and dot..

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Nov 2 18:54:56 CET 2007

On Fri, 02 Nov 2007 16:39:12 +0000, Roberto Bonvallet wrote:

> On 31 oct, 22:21, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
>> def convert(n):
>>    assert type(n) in (int,long)
> I'd replace this line with  n = int(n), more in the spirit of duck
> typing.

Not necessarily. Something duck typing is too liberal in what it accepts. 
You might want convert(math.pi) to raise an exception, although I'd 
suggestion an assert is the wrong test. A better test would be an 
explicit type check with raise:

if not isinstance(n, (int, long)):
    raise TypeError('n not an integer')

assert is for "this can never happen" tests rather than type checking.


More information about the Python-list mailing list