Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101944 > unrolled thread

Same function but different names with different set of default arguments

Started byPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
First post2016-01-21 07:30 +0000
Last post2016-01-23 20:03 +0000
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Same function but different names with different set of default arguments Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> - 2016-01-21 07:30 +0000
    Re: Same function but different names with different set of default arguments Yann Kaiser <kaiser.yann@gmail.com> - 2016-01-21 07:49 +0000
    Re: Same function but different names with different set of default arguments Yann Kaiser <kaiser.yann@gmail.com> - 2016-01-21 08:45 +0000
    Re: Same function but different names with different set of default arguments Peter Otten <__peter__@web.de> - 2016-01-21 10:26 +0100
    Re: Same function but different names with different set of default arguments Steven D'Aprano <steve@pearwood.info> - 2016-01-21 23:24 +1100
    Re: Same function but different names with different set of default arguments Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt> - 2016-01-23 20:03 +0000

#101944 — Same function but different names with different set of default arguments

FromPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
Date2016-01-21 07:30 +0000
SubjectSame function but different names with different set of default arguments
Message-ID<n7q1ds$11rf$1@gioia.aioe.org>
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)

Thanks
Paulo

[toc] | [next] | [standalone]


#101945

FromYann Kaiser <kaiser.yann@gmail.com>
Date2016-01-21 07:49 +0000
Message-ID<mailman.138.1453362607.15297.python-list@python.org>
In reply to#101944
partial treats keyword arguments as default values, though they become
keyword-only as a result :

f1 = functools.partial(g, p="p1")

On Thu, Jan 21, 2016, 08:35 Paulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
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)
>
> Thanks
> Paulo
> --
> https://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#101950

FromYann Kaiser <kaiser.yann@gmail.com>
Date2016-01-21 08:45 +0000
Message-ID<mailman.142.1453365911.15297.python-list@python.org>
In reply to#101944
Apparently the thread poster cannot receive email, I just received a bounce
on my previous email :-/

On Thu, Jan 21, 2016, 08:49 Yann Kaiser <kaiser.yann@gmail.com> wrote:

> partial treats keyword arguments as default values, though they become
> keyword-only as a result :
>
> f1 = functools.partial(g, p="p1")
>
> On Thu, Jan 21, 2016, 08:35 Paulo da Silva <
> p_s_d_a_s_i_l_v_a_ns@netcabo.pt> 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)
>>
>> Thanks
>> Paulo
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>

[toc] | [prev] | [next] | [standalone]


#101953

FromPeter Otten <__peter__@web.de>
Date2016-01-21 10:26 +0100
Message-ID<mailman.144.1453368402.15297.python-list@python.org>
In reply to#101944
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 ;)

[toc] | [prev] | [next] | [standalone]


#101960

FromSteven D'Aprano <steve@pearwood.info>
Date2016-01-21 23:24 +1100
Message-ID<56a0cdf9$0$1609$c3e8da3$5496439d@news.astraweb.com>
In reply to#101944
On Thu, 21 Jan 2016 06:30 pm, Paulo da Silva wrote:

> Hi all.
> 
> What is the fastest implementation of the following code?

Let's find out. Here are three different ways to do it:

def g(p):
    return

def f1(p=3):  # argument with a default
    return g(p)

def f2():  # no argument at all
    return g(3)

from functools import partial
f3 = partial(g, 3)


# setup timing code
from timeit import Timer
t1 = Timer("f1()", "from __main__ import f1")
t2 = Timer("f2()", "from __main__ import f2")
t3 = Timer("f3()", "from __main__ import f3")


Now let's see how long they take. This is using Python 2.7 on my computer.


py> min(t1.repeat(repeat=7))
0.43099188804626465
py> min(t1.repeat(repeat=7))
0.4344518184661865
py> min(t1.repeat(repeat=7))
0.42992687225341797



py> min(t2.repeat(repeat=7))
0.43400001525878906
py> min(t2.repeat(repeat=7))
0.432689905166626
py> min(t2.repeat(repeat=7))
0.43417787551879883


py> min(t3.repeat(repeat=7))
0.281879186630249
py> min(t3.repeat(repeat=7))
0.27957892417907715
py> min(t3.repeat(repeat=7))
0.28043699264526367


There's no visible difference between the first and second method. The third
method, using functools.partial, is considerably faster, BUT remember that
this only effects the time it takes to call the function g(). If g()
actually does any work, the time spent doing the work will *far* outweigh
the overhead of calling the function.




-- 
Steven

[toc] | [prev] | [next] | [standalone]


#102057

FromPaulo da Silva <p_s_d_a_s_i_l_v_a_ns@netcabo.pt>
Date2016-01-23 20:03 +0000
Message-ID<n80mat$a0p$1@gioia.aioe.org>
In reply to#101944
Às 07:30 de 21-01-2016, Paulo da Silva escreveu:
> 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)
> 

Thanks to all who responded.

I'll try "partial" in my case.
It seems good and simple.

Paulo

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web