[Python-ideas] op-assign in function argument

Nick Coghlan ncoghlan at gmail.com
Wed Feb 24 14:57:44 CET 2010


Calvin Spealman wrote:
> On Wed, Feb 24, 2010 at 1:23 AM, wxyarv <weasley_wx at qq.com> wrote:
>> has anybody thought the op-assign in function argument is a good idea? now
>> we can use parameter name to specifie argument, like this:
>>
>> def foo(arg1): pass
>>
>> # call
>> foo(arg1 = "bar")
>>
>> but, if function has default parameter, it will be inconvenience:
>> def foo_with_long_defualts(arg1 = we | are | default | flags | set): pass
>>
>> # call
>> foo_with_long_defaults(arg1 = we | are | default | flags | set |
>> another_flags)
>>
>> so, why not add op-assign support in named argument?
>>
>> foo_with_long_defaults(arg1 |= another_flags)
> 
> If you wan to do this, you can just support it in the function
> yourself. That is, if the function wants to take an additional_flags
> parameter, it should. I don't think the complications this could add
> to the call syntax are good. It becomes a form of
> assignment-as-an-expression and makes the call harder to parse (by
> human eyes).

Even better is to just give the default args a name:

FOO_DEFAULTS = we | are | default | flags | set
def foo(arg=FOO_DEFAULTS):
  ...

my_flags = FOO_DEFAULTS | other_flags
foo(arg=my_flags)

(Also -1 on this syntax idea - simply giving the default value a name is
a much simpler and more obvious approach)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------



More information about the Python-ideas mailing list