[Python-Dev] Next-to-last wart in Python syntax.

Finn Bock bckfnn@worldonline.dk
Wed, 14 Mar 2001 11:49:54 GMT


>barry wrote:
>>
>>    | - and as keyword argument names in arglist
>>
>> I think this last one doesn't work:
>> 
>> -------------------- snip snip --------------------
>> Jython 2.0 on java1.3.0 (JIT: jitc)
>> Type "copyright", "credits" or "license" for more information.
>> >>> def foo(class=None): pass
>> Traceback (innermost last):
>>   (no code object) at line 0
>>   File "<console>", line 1
>> def foo(class=None): pass
>>         ^
>> SyntaxError: invalid syntax
>> >>> def foo(print=None): pass
>> Traceback (innermost last):
>>   (no code object) at line 0
>>   File "<console>", line 1
>> def foo(print=None): pass
>>         ^
>> SyntaxError: invalid syntax
>> -------------------- snip snip --------------------

[/F]

>>>> def spam(**kw):
>...     print kw
>...
>>>> spam(class=1)
>{'class': 1}
>>>> spam(print=1)
>{'print': 1}

Exactly.

This feature is mainly used by constructors for java object where
keywords becomes bean property assignments.

  b = JButton(text="Press Me", enabled=1, size=(30, 40))

is a shorthand for

  b = JButton()
  b.setText("Press Me")
  b.setEnabled(1)
  b.setSize(30, 40)

Since the bean property names are outside Jython's control, we allow
AnyName in that position.

regards,
finn