"" <> 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]
|