[Python-ideas] Specifying constants for functions
Bruce Leban
bruce at leban.us
Tue Oct 27 15:02:25 EDT 2015
On Tue, Oct 27, 2015 at 11:18 AM, Guido van Rossum <guido at python.org> wrote:
> IIRC it's an old micro-optimization; this was a common idiom at Zope. But
> I think it's way overused -- people believe it works so they do it all the
> time, even for code that's not performance sensitive, just because it's
> become a habit. (Like "register" in C in the '80s.)
>
> On Tue, Oct 27, 2015 at 10:55 AM, Yury Selivanov <yselivanov.ml at gmail.com>
> wrote:
>
>> Serhiy,
>>
>> On 2015-10-27 1:45 PM, Serhiy Storchaka wrote:
>>
>>> There is known trick to optimize a function:
>>>
>>> def foo(x, y=0, len=len, pack=struct.pack, maxsize=1<<BPF):
>>> ...
>>
>>
It's not a micro-optimization in this case:
def foo(x, SENTINEL=object()):
...
I don't like mangling function signatures to do this. What I really want is
the equivalent of C's static here:
def foo(x):
static SENTINEL = object()
...
This has two important semantics: (1) the scope of the SENTINEL variable is
limited to the function foo; (2) it is only initialized once.
Hosting the value out of the function into a decorator like @asconstants is
fine.
--- Bruce
Check out my new puzzle book: http://J.mp/ingToConclusions
<http://j.mp/ingToConclusions>
Get it free here: http://J.mp/ingToConclusionsFree
<http://j.mp/ingToConclusionsFree> (available on iOS)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151027/868bbc29/attachment.html>
More information about the Python-ideas
mailing list