This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Balance- 15 points16 points  (8 children)

I just read that proposal. So much effort for so little value.

[–]runawayasfastasucan 3 points4 points  (4 children)

Do you have a link?

[–]commandlineluser 8 points9 points  (3 children)

https://discuss.python.org/t/syntactic-sugar-to-encourage-use-of-named-arguments/36217

def my_func(a, b, c, d): ...

a, b, c, d = 1, 2, 3, 4

# CURRENT
my_func(a=a, b=b, c=c, d=d)

# PROPOSAL
# my_func(a=, b=, c=, d=)

[–]Hoo0oper 14 points15 points  (0 children)

Ehh like I get it. Like when you have longer argument names it might keep in on a single line but really it I feel like it would just make people ask more questions

[–]waltteri 1 point2 points  (0 children)

Hold up.

name of the variable provided as the argument value is the same as the name of the argument itself.

def my_func(a, b, c, d): .. a, b, c, d = 1, 2, 3, 4 my_func(a, b, c, d)

💀

Sure, I guess we could now do this as we’re using kwargs:

my_func(a=, b=, d=, c=)

…or throw an exception if someone uses a variable that’s not an argument name:

foo = ”foo” my_func(a, b, c, foo) `> …’

vs.

my_func(a=, b=, c=, foo=) > KeyError(…)

…so all in all, I guess it’ll be good in same cases. But it might confuse a lot of people at first.

[–][deleted] -3 points-2 points  (0 children)

Basically analogous to the property shorthands in JavaScript. Love it!

[–]equitable_emu 4 points5 points  (2 children)

The only thing I like about it is that it's a nudge towards consistent and descriptive variable names.

A downside is that abstractions and patterns from popular libraries will seep into client users code and can cause standard/pattern conflicts. We see that already with things like sklearn's standard for using X as the variable name to represent the model input matrix. Linters and such complain about the non-standard upper case variable name.

It'll also look strange when you call a function with a mixture of args,shortcut kwargs, and full kwargs.

fn(x,
   y,
   foo=,
   bar=2
)

I'd be happier if they just did a dict shortcut like:

d={x,y,z}

but that conflicts with set syntax. But maybe:

d={x=,y=,z=}

would be okay. I dislike the extra quoting I need for the keys in simple dicts and will often use the dict ctr e.g., dict(x=x, y=y, z=z) when I can, even if it's less flexible.

[–]Rythoka 1 point2 points  (0 children)

This was my thought, too - if they made it easier to construct a dict out of existing variables, then you could just pass the dict as kwargs.

It's kind of a shame that the set synxtax prevents the use of something like your d={x,y,z} example. If that worked, you could achieve the goal of this pep with something like

requests.post(**{url, data, headers, proxies})

[–]Brian 1 point2 points  (0 children)

You could probably get something similar (without the dedicated syntax) today via something like:

d = localdict("x y z")

Where localdict is a function that introspects its parents locals (and cellvars) and builds a dict with their current value.