[Baypiggies] Newbie-ish type questions...
Chad Netzer
chad.netzer at gmail.com
Wed Apr 23 18:49:23 CEST 2008
On Wed, Apr 23, 2008 at 9:29 AM, Paul McNett <p at ulmcnett.com> wrote:
> Kelly Yancey wrote:
>
> > But, yes, it gets better in 3.0:
> > http://www.python.org/dev/peps/pep-3119/
> >
>
> Now *that* is reason alone to migrate to 3.0.
>
> def myfunc(seq=None):
> if seq is None:
> seq = ()
> assert isinstance(seq, Sequence)
And in the meantime, it illustates that (generally) you should test
for behaviors, not type. The 'operator' module has some useful
predicates:
isSequenceType()
isNumberType()
isMappingType()
So the assertion above becomes:
assert operator.isSequenceType(seq)
If you need a sequence to be a list, just convert it:
seq = list(seq)
With these tools, you should almost never use "type(a) is list", or
similar, in your code.
* Note - I have no idea how isSequenceType() deals with iterators or
generators... Does someone have a few moments to test this?
More information about the Baypiggies
mailing list