If you ask if Python can overload the method: no. Therefore, executing several methods like this is a rather non-pythonic way of defining a method. In addition, a naming convention typically refers to upper case class names, as well as lower case functions / methods.
If you want to go forward, the easiest way would be to simply create a branch:
def Factorial(arg): if getattr(arg, '__iter__', False):
Or, if you feel weird, you can make a decorator that does this with any function:
def autoMap(f): def mapped(arg): if getattr(arg, '__iter__', False): return [mapped(x) for x in arg] else: return f(arg) return mapped @autoMap def fact(x): if x == 1 or x == 0: return 1 else: return fact(x-1) + fact(x-2) >>> fact(3) 3 >>> fact(4) 5 >>> fact(5) 8 >>> fact(6) 13 >>> fact([3,4,5,6]) [3, 5, 8, 13]
Although the more Pythonic way is to use variable argument lengths:
def autoMap2(f): def mapped(*arg): if len(arg) != 1: return [f(x) for x in arg] else: return f(arg[0]) return mapped @autoMap2 def fact(n):
Put the two together in the deep image decorator:
def autoDeepMap(f): def mapped(*args): if len(args) != 1: return [mapped(x) for x in args] elif getattr(args[0], '__iter__', False): return [mapped(x) for x in args[0]] else: return f(args[0]) return mapped @autoDeepMap def fact(n):