type of simple object
Jacek Generowicz
jacek.generowicz at cern.ch
Tue Feb 1 11:31:03 EST 2005
"ajikoe at gmail.com" <ajikoe at gmail.com> writes:
> Hello Jacek,
>
> Thanks for the answer,
>
> Can you tell me how can I check if an object is a sequence (you are
> right, this is actually what I want)?
You try to use it as a sequence. If it works, then it was a
sequence. If it was not a sequence, you handle the exception and do
something appropriate.
For example:
>>> def f(seq, something):
... try:
... number = sum(something)
... except TypeError:
... number = something
... return [number*item for item in seq]
...
>>> f([1,2,3], 4)
[4, 8, 12]
>>> f([1,2,3], [1,2,3])
[6, 12, 18]
>>> f([1,2,3], (1,2,3))
[6, 12, 18]
>>> f([1,2,3], {1:'one', 2:'two', 3:'three'})
[6, 12, 18]
More information about the Python-list
mailing list