Same function but different names with different set of default arguments

Peter Otten __peter__ at web.de
Thu Jan 21 04:26:28 EST 2016


Paulo da Silva wrote:

> Hi all.
> 
> What is the fastest implementation of the following code?
> 
> def g(p):
> ...
> return something
> 
> def f1(p="p1"):
> return g(p)
> 
> def f2(p="p2"):
> return g(p)

>>> def g(p): return p.upper()
... 
>>> def f1(): pass
... 
>>> f1.__code__ = g.__code__
>>> f1.__defaults__ = ("p1",)
>>> f1()
'P1'

Not recommended ;)




More information about the Python-list mailing list