function taking scalar or list argument

beliavsky@aol.com beliavsky at 127.0.0.1
Mon Aug 23 19:39:26 CEST 2004


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? I intend to define many functions like this and want
to use the right method. Thanks.



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---



More information about the Python-list mailing list