function taking scalar or list argument

Robert Brewer fumanchu at amor.org
Mon Aug 23 14:07:45 EDT 2004


beliavsky wrote:
> I can define a function that transforms either a scalar or 
> each element in
> a list as follows:
> 
> def twice(x):
>     try:
>         return map(twice,x)
>     except:
>         return 2*x
> 
> print twice(3) # 6
> print twice([1,4,9]) # [2,8,18]
> 
> Is this good style?

If you mention that behavior in a docstring, it's acceptable.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list