specifying constants for a function (WAS: generator expressions: performance anomaly?)

Bengt Richter bokr at oz.net
Mon Jan 24 15:35:09 EST 2005


On Mon, 24 Jan 2005 00:31:17 -0700, Steven Bethard <steven.bethard at gmail.com> wrote:

>Bengt Richter wrote:
>> So, e.g., for
>> 
>>  >>> presets = dict(a=1, b=2, deftime=__import__('time').ctime())
>> 
>> in the decorator args, the next version will act as if the decorated
>> function had the source code
>> 
>>  >>> print '%s = __frompresets__' % ', '.join(sorted(presets))
>>  a, b, deftime = __frompresets__
>> 
>> for the current version, except that it will be hidden.
>
>Cool!  Keep us posted.  I assume you'll put this into the Cookbook?
>
Not ready for prime time, I guess, but I made a module that does presets
and also adjusts parameters and count to do currying without nested function
calling in the curried function.

 >>> from presets import presets, curry
 >>> @presets(verbose=True, a=1, b=2, deftime=__import__('time').ctime())
 ... def foo():
 ...    print (a, b)
 ...    print deftime
 ...

 presets: -- "name(?)" means name may be unused
            a = 1
            b = 2
      deftime = 'Mon Jan 24 12:16:07 2005'

 >>> foo()
 (1, 2)
 Mon Jan 24 12:16:07 2005
 >>> @curry(verbose=True, y=444)
 ... def bar(x=3, y=4):
 ...     return x*y
 ...

 presets: -- "name(?)" means name may be unused
            y = 444

 >>> bar(2)
 888
 >>> bar(2, 3)
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 TypeError: bar() takes at most 1 argument (2 given)
 >>> bar()
 1332
 >>> import dis
 >>> dis.dis(bar)
   1           0 LOAD_CONST               1 (444)
               3 STORE_FAST               1 (y)

   3           6 LOAD_FAST                0 (x)
               9 LOAD_FAST                1 (y)
              12 BINARY_MULTIPLY
              13 RETURN_VALUE
 >>> dis.dis(foo)
   1           0 LOAD_CONST               1 ((1, 2, 'Mon Jan 24 12:16:07 2005'))
               3 UNPACK_SEQUENCE          3
               6 STORE_FAST               0 (a)
               9 STORE_FAST               1 (b)
              12 STORE_FAST               2 (deftime)

   3          15 LOAD_FAST                0 (a)
              18 LOAD_FAST                1 (b)
              21 BUILD_TUPLE              2
              24 PRINT_ITEM
              25 PRINT_NEWLINE

   4          26 LOAD_FAST                2 (deftime)
              29 PRINT_ITEM
              30 PRINT_NEWLINE
              31 LOAD_CONST               0 (None)
              34 RETURN_VALUE

Now I have to see whether I can optimize the result with Raymond's make_constants
as two decorators in a row... (pre without keyword args is an alias for Raymond's make_constants,
just handy for the moment)

 >>> from pre import pre
 >>> @pre(verbose=True)
 ... @presets(verbose=True, a=1, b=2, deftime=__import__('time').ctime())
 ... def foo():
 ...    print (a, b)
 ...    print deftime
 ...    print __name__
 ...

 presets: -- "name(?)" means name may be unused
            a = 1
            b = 2
      deftime = 'Mon Jan 24 12:29:21 2005'

 __name__ --> __main__
 >>> foo()
 (1, 2)
 Mon Jan 24 12:29:21 2005
 __main__
 >>> dis.dis(foo)
   1           0 LOAD_CONST               1 ((1, 2, 'Mon Jan 24 12:29:21 2005'))
               3 UNPACK_SEQUENCE          3
               6 STORE_FAST               0 (a)
               9 STORE_FAST               1 (b)
              12 STORE_FAST               2 (deftime)

   3          15 LOAD_FAST                0 (a)
              18 LOAD_FAST                1 (b)
              21 BUILD_TUPLE              2
              24 PRINT_ITEM
              25 PRINT_NEWLINE

   4          26 LOAD_FAST                2 (deftime)
              29 PRINT_ITEM
              30 PRINT_NEWLINE

   5          31 LOAD_CONST               2 ('__main__')
              34 PRINT_ITEM
              35 PRINT_NEWLINE
              36 LOAD_CONST               0 (None)
              39 RETURN_VALUE

Regards,
Bengt Richter



More information about the Python-list mailing list