YAI: syntax for preset locals without using dummy args with defaults

Cliff Wells LogiplexSoftware at earthlink.net
Fri Jan 10 16:43:23 CET 2003


On Fri, 2003-01-10 at 07:22, Bengt Richter wrote:
> --------------------------------------------------------------------------------
> [1] Something like
> 
>     def foo(x, y=default):
>         presets:
>             z = something
>             pi = __import__('math').pi
>             twopi = 2.0*pi # not possible in parameter list version
>             if z < 0: raise ValueError, 'Def-time preset check, possible with presets: block'
>         if x<0 or y<0: raise ValueError, 'Call-time parameter check'
>         return pi*(x*x-y*y)
> 
> I thought the first version above might be easier to implement, but
> either way, having a sanctioned way to accomplish safe presetting of
> function locals should make for a performance boost in many cases.
> 
> I think either version will be interesting for method definitions too.

If I understand your intent (only initializing the 'presets' a single
time), you can get the same performance boost using a class:

class __foo:
    # presets
        z = 1
    pi = __import__('math').pi
    twopi = 2.0*pi # not possible in parameter list version
    if z < 0: raise ValueError, 'Def-time preset check, possible with
presets: block'

    def __call__(self, x, y = 1):
        if x<0 or y<0: raise ValueError, 'Call-time parameter check'
        return self.pi*(x*x-y*y)

foo = __foo()

print foo(1, 2)
print foo(3, 5)


Obviously the downside to this approach is the 'superfluous' __foo
definition that won't be used again, but this works now and doesn't
require any new syntax.


Regards,

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308






More information about the Python-list mailing list