Initializing with the correct type

Istvan Albert istvan.albert at gmail.com
Thu Dec 7 10:30:08 EST 2006


aine_canby at yahoo.com wrote:
> Hi all,
>
> I'm new to Python and I'm just wordering if my approch is correct.
> Here's an example. I'm making sure that the length and types are
> correct.

Don't worry much about the type of parameters, it turns out passing
wrong data into a constructor is a problem that is both easy to avoid
and catch once it happens. Here is also an alternative to your code

try:
   assert len(data) in (1, size), 'incorrect size'
   assert len(data)==len( [ x for x in data if type(x)==int ] ),
'noninteger data in the list'
except Exception, exc:
   raise FormatError('Data error: %s' % exc)

i.




More information about the Python-list mailing list