[Python-ideas] Decorators that execute once rather than every import (was: codec and decorator hacks)
INADA Naoki
songofacandy at gmail.com
Fri Aug 22 13:50:52 CEST 2014
Longer import time makes CLI applications slower.
It's a real problem for people writing command line application.
For example, both of mercurial and Bazaar has lazy import system to
minimize import.
On Fri, Aug 22, 2014 at 4:41 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Thu, Aug 21, 2014 at 11:42:59PM -0700, Gregory P. Smith wrote:
>> On Wed, Aug 20, 2014 at 7:25 AM, Donald Stufft <donald at stufft.io> wrote:
>>
>> >
>> > mypy does have a codec that will ignore annotations on 2.x.
>> >
>>
>> Note that all codec hacks and decorator hacks have a down side other than
>> being hacks: They significantly slow down program import and start time. At
>> least the results of the codec hack are cached when a .pyc is generated.
>> Decorator executions are not.
>
> Decorator executions are no different from any other function call. They
> occur when you call them. If you only call the decorator once, at the
> top level of the module, they don't get called again when you import the
> module again. That's no different from any other code at the top-level:
>
>
> [steve at ando ~]$ cat test_deco.py
> def decorate(func):
> print("calling an expensive decorator...")
> func.__name__ = "decorated_%s" % func.__name__
> return func
>
> @decorate
> def spam(n):
> return "spam"*n
>
> print("%s: %s" % (spam.__name__, spam(3)))
>
> [steve at ando ~]$ python3.3
> Python 3.3.0rc3 (default, Sep 27 2012, 18:44:58)
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> py> import test_deco as d
> calling an expensive decorator...
> decorated_spam: spamspamspam
> py> import test_deco # importing again doesn't re-call the decorator
> py> test_deco.spam.__name__
> 'decorated_spam'
>
>
>> hmm.. could a flavor of decorator that is cacheable in .pyc's be created so
>> the cost is only paid once rather than for each time the module is imported?
>
> I'm not sure that you're trying to solve a real problem. Can you give
> an example?
>
>
>
> --
> Steven
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
--
INADA Naoki <songofacandy at gmail.com>
More information about the Python-ideas
mailing list