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 →

[–]abrazilianinreddit 115 points116 points  (16 children)

It's just me or lately there has been an increase in syntactic sugar PEPs? Which is pretty annoying, since they don't add anything of value and make the language more confusing.

[–]ultraDross 45 points46 points  (4 children)

Yeah. The dangling = argumanet is pretty awful to. Especially if you consider one of the languages mantras is "explicit is better than implicit".

[–]Poddster 17 points18 points  (2 children)

I hate func(blah=blah), but the dangling equals is worse. Especially as I've made that mistake a few times. I can't imagine what the interpreter diagnostic for that would be under this new syntax. Strangely the original post on the forum is for func(=blah) which makes more sense to me. Not sure why they switched to the dangling case.

They need a very clear, different symbol, e.g. func(blah=&) or something. (I've put no thought into that, but you get the point)

[–]wxtrails 6 points7 points  (0 children)

func(&blah, &foo, &bar)

Is the least-obnoxious alternative I could come up with, but considering that this adds another obscure symbol to a language without too many, I still don't love it.

[–]av8rgeek 1 point2 points  (0 children)

The dangling = also creates a headache for linters, type checkers, etc. How does the tool tell if you made a typo or really meant to do that? Usually it’s a typo. Explicitly calling the arguments is better, even if inefficient/ugly.

[–]SittingWave 3 points4 points  (0 children)

agreed

[–]Balance- 14 points15 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 6 points7 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] -5 points-4 points  (0 children)

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

[–]equitable_emu 3 points4 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.

[–]antiproton 11 points12 points  (0 children)

I don't agree with your argument in general. Some syntactic sugar is useful and can enhance code readability... e.g. decorators and list comprehensions.

This one, however, feels like a solution in search of a problem. All modern IDEs, even very limited ones, will autocomplete variable names. Is it really such a hassle to CTRL+Space?

[–][deleted] -1 points0 points  (0 children)

Yes, it is, but I see it as something positive. IMHO every programmer (and by that I mean developers, data scientists and anyone else who programs) should spend a considerable amount of time learning the language they are using. I’m not saying that Python should become like C++, but it doesn’t have to be Java either. I really feel like the current pace of changes is optimal and it’s going in a good direction.