"no variable or argument declarations are necessary."

Bengt Richter bokr at oz.net
Thu Oct 6 17:55:37 EDT 2005


On 6 Oct 2005 06:44:41 GMT, Antoon Pardon <apardon at forel.vub.ac.be> wrote:

>Op 2005-10-06, Bengt Richter schreef <bokr at oz.net>:
>> On 5 Oct 2005 09:27:04 GMT, Duncan Booth <duncan.booth at invalid.invalid> wrote:
>>
>>>Antoon Pardon wrote:
>>>
>>>> It also is one possibility to implement writable closures.
>>>> 
>>>> One could for instace have a 'declare' have the effect that
>>>> if on a more inner scope such a declared variable is (re)bound it
>>>> will rebind the declared variable instead of binding a local name.
>>>
>>>That is one possibility, but I think that it would be better to use a 
>>>keyword at the point of the assigment to indicate assignment to an outer 
>>>scope. This fits with the way 'global' works: you declare at (or near) the 
>>>assignment that it is going to a global variable, not in some far away part 
>>>of the code, so the global nature of the assignment is clearly visible. The 
>>>'global' keyword itself would be much improved if it appeared on the same 
>>>line as the assignment rather than as a separate declaration.
>>>
>>>e.g. something like:
>>>
>>>var1 = 0
>>>
>>>def f():
>>>  var2 = 0
>>>
>>>  def g():
>>>     outer var2 = 1 # Assign to outer variable
>>>     global var1 = 1 # Assign to global
>>
>> IMO you don't really need all that cruft most of the time. E.g., what if ':='
>> meant 'assign to variable wherever it is (and it must exist), searching according
>> to normal variable resolution order (fresh coinage, vro for short ;-), starting with
>> local, then lexically enclosing and so forth out to module global (but not to builtins).'
>
>Just some ideas about this
>
>1) Would it be usefull to make ':=' an expression instead if a
>   statement?
Some people would think so, but some would think that would be tempting the weak ;-)

>
>I think the most important reason that the assignment is a statement
>and not an expression would apply less here because '==' is less easy
>to turn into ':=' by mistake than into =
>
>Even if people though that kind of bug was still too easy
>
>2) What if we reversed the operation. Instead of var := expression,
>   we write expression =: var.
>
>IMO this would make it almost impossible to write an assignment
>by mistake in a conditional when you meant to test for equality.
It's an idea. You could also have both, and use it to differentiate
pre- and post-operation augassign variants. E.g.,

    alist[i+:=2] # add and assign first, index value is value after adding

    alist[i=:+2] # index value is value before adding and assigning

Some people might think that useful too ;-)

Hm, I wonder if any of these variations would combine usefully with the new
short-circuiting expr_true if cond_expr else expr_false ...

Sorry I'll miss the flames, I'll be off line a while ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list