[Python-ideas] Before and after the colon in funciton defs.

Devin Jeanpierre jeanpierreda at gmail.com
Thu Sep 22 17:18:50 CEST 2011


> Decorators could be a good way to do this, but the problem in these
> cases, is the function object doesn't have the needed support to make
> things like this easy.
>
>
> Probably the easiest and most direct way, would to be to add a new
> keyword 'static' as MRAB suggested, but have it be an expression instead
> of a command.

I hope I'm not bikeshedding here, but I much prefer decorators because
they let you do more than just micro-optimization. They were mentioned
before, in another thread, and I find them attractive.

A decorator basically lets you inject locals into already-existing
functions. So if you want to, e.g., test a function that uses a global
"urlopen" function to download a file, by replacing urlopen.

    from urllib import urlopen
    from StringIO import StringIO

    def myfunction():
        return urlopen('...').read()

    class TestMyFunction(unittest.TestCase):
        def setUp(self):
            self.func = inject(urlopen=lambda s: StringIO(s)

        def test_opens(self):
            self.assertEqual(self.func(), '...')

There's also the benefits of not altering the language etc. -- it's
just a function that returns a new function that's very similar to the
old one. That simplicity appeals to me as well. And it still satisfies
the use-case of the new "static" keyword.

Devin

On Wed, Sep 21, 2011 at 2:12 PM, Ron Adam <ron3200 at gmail.com> wrote:
> On Wed, 2011-09-21 at 15:19 +1000, Nick Coghlan wrote:
>
>> Perhaps a non-syntax way to approach both of these would be to add a
>> new decorator to 'functools':
>>
>>     def closure(f):
>>         """Invokes the decorated function and returns the result after
>> transcribing essential function metadata
>>
>>            This can be used to easily share algorithm state and get
>> early binding semantics for names.
>>         """
>>         impl = f()
>>         impl.__name__ = f.__name__
>>         doc = f.__doc__
>>         if doc is not None:
>>             impl.__doc__ = doc
>>         impl.__dict__.update(f.__dict__)
>>         return impl
>>
>> This would be used as follows:
>>
>>     @functools.closure
>>     def adder(i=i): # 'impl' defines call time signature
>>         "Increments 'x' by adder.value"
>>         def impl(x):
>>             impl.call_count += 1
>>             return x + i
>>         impl.value = i
>>         impl.call_count = 0
>>         return impl
>>
>> >>> adder.value
>> 10
>> >>> adder(1)
>> 11
>> >>> adder(5)
>> 15
>> >>> adder(10)
>> 20
>> >>> adder.call_count
>> 3
>
> Simplifying things like this is one of the use cases of allowing define
> time statements.  That's a lot of work to just avoid putting a keyword
> in the signature.  And it's not easy to understand.
>
> Decorators could be a good way to do this, but the problem in these
> cases, is the function object doesn't have the needed support to make
> things like this easy.
>
>
> Probably the easiest and most direct way, would to be to add a new
> keyword 'static' as MRAB suggested, but have it be an expression instead
> of a command.
>
> value = (static <expression>)
>
>
> def adder(x):
>    return x + (static i)    # evaluate (static i) at compile time.
>
> The parentheses would be optional.
>
> The (static i) expression, could be spelled (i=i).  I think that was
> what Guido was suggesting, but not as an expression.  As an expression,
> you would then see things like.. i = (i=i).  But that may only be poor
> style, because it's easy enough to just not do that.
>
> I think the expression form is better myself, it allows you to get both
> the compile time value, and the current value of an identifier.
>
>
> def value elapsed_frames():
>    """ Where f is the frame counter in the parent scope. """
>    return f - (static f)
>
>
> Cheers,
>   Ron
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>



More information about the Python-ideas mailing list