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

Ron Adam ron3200 at gmail.com
Thu Sep 22 09:47:27 CEST 2011


On Wed, 2011-09-21 at 18:21 -1000, Carl Matthew Johnson wrote:
> Well, now that we're getting down to picking bike shed colors, time to chime in:
> 
> blah = static blah
> 
> is worse than 
> 
> static blah = blah

Which way, will depend on what kind of behavior is decided on.  

Statement? or Expression?


As a statement, it would be first and not have the assignment.

    static blah

(if it does, then working out how that works will be tricky.)



As an expression, the only thing that matters is that the names used in
the expression, are findable at define time.  No assignment is needed.
The value takes the place of the static expression when the function it
is in is called.

Here the expression "(static y)" is replaced with the object y was bound
to at the time the function was defined.
 
    y = ['red', 'blue', 'green']
    ...
    def f()
        ...
        for x in (static y):
        ...
    
The parentheses are optional if the expression only has single value.
But I think it makes it easier to see what is being done.


 An expression you can be used in lambdas.  A statement can't.

Static statement:

    def get_adders(count):
        adders = []
        for n in range(count):
            def add_it(x):
                static n
                return n + x
            adders.append(add_it)
        return adders


Static expression:

    def get_adders(count):
        adders = []
        for n in range(count):
            adders.append(lambda x: return (static n) + x) 
        return adders


      

> One question I have is how this will work with control flow. This example is works with Python 2.7 and 3.2:
> 
> >>> x = 0
> >>> def f():
> ...  if False: global x
> ...  x = 7
> ...  return x
> ... 
> >>> f()
> 7
> >>> x
> 7
> 
> Presumably static would work the same way? Still, I find this sort of distasteful,
> since the behavior is a bit surprising to those not familiar with the in's and out's
> of the compilation model. 

I can see why that is surprising.

I don't think the static would have this issue in either form.

Cheers,
    Ron





















More information about the Python-ideas mailing list