[Python-ideas] Short form for keyword arguments and dicts

Steven D'Aprano steve at pearwood.info
Sun Jun 23 14:22:16 CEST 2013


On 23/06/13 21:17, Anders Hovmöller wrote:

> Objective-C is better in this case because it strongly enforces something
> like keyword argument always. What I'm saying is that it'd be nice to be
> able to write code that uses keyword arguments 100% of the time for all
> function calls without making the readability worse.

Nobody is stopping you from using keyword arguments 100% of the time (except for built-in functions that don't accept keyword arguments, but most of them take only one or two arguments). Go right ahead. I love keyword arguments! But this discussion isn't about the pros and cons of keyword arguments. This discussion is about adding magic syntax for implicitly specifying the keyword parameter name when it happens to match an argument which is an expression consisting of a single name.

With your suggestion, you can abbreviate this special case:

create_user(first_name=first_name, last_name=last_name, contact_email=contact_email)

with this:

create_user(=first_name, =last_name, =contact_email)

(which I consider too ugly for words), but it does absolutely nothing for:


create_user(first_name=record[3], last_name=record[2], contact_email=record.email)

create_user(first_name=personal_name, last_name=family_name, contact_email=email_address)

create_user(first_name="Steven", last_name="D'Aprano", contact_email="steve at example.com")

create_user(first_name=first_name.title(), last_name=last_name.title(), contact_email=validate_and_clean(contact_email))


The special case "parameter name matches exactly argument expression" is far too special, and the benefit far too minor, to deserve special syntax.

Oh, one last thing... your suggestion is also brittle. If you refactor the variable name, or change the function parameter name, code using this shortcut will break. Parameter names are part of the function API and shouldn't change, but variable names are not, and should be free to change. With your suggestion, they can't.



-- 
Steven


More information about the Python-ideas mailing list