Most active commenters
  • roel_v(4)
  • mehrdadn(3)

←back to thread

317 points est | 25 comments | | HN request time: 1.977s | source | bottom
1. roel_v ◴[] No.17448647[source]
Having just spend the last few weeks writing Python, this comment will come off as bitter, but - really? Out of all the shitty syntax things, this sort of thing is what they're willing to fix?
replies(7): >>17448692 #>>17448695 #>>17448702 #>>17448723 #>>17448807 #>>17448862 #>>17448928 #
2. isoprophlex ◴[] No.17448692[source]
Can you elaborate what other syntax decisions you find shitty? In general I find the syntax of python to be very clean and easy to understand
3. vanderZwan ◴[] No.17448695[source]
It would be more constructive if you added explanations of what other (in your opinion) "shitty syntax" things you prefer to see addressed and why.

EDIT: also, I'm mentioning "in your opinion" because adding that to your own statements indicates that you're open to discussion. It's also good to use it as a reminder to yourself (speaking as a former physics student, and most people who know physics students will agree how "absolute" and unintentionally arrogant they tend to be in their claims until they learn otherwise).

I'm sure your coding experience was frustrating, and I'm sorry to hear that, and I understand that we all need to vent sometimes, but trying to staying open to other viewpoints is better for your own sanity, wisdom, and social connections in the long run.

replies(1): >>17448734 #
4. hyperbovine ◴[] No.17448702[source]
Like what? Python generally has pretty good syntax.
5. icebraining ◴[] No.17448723[source]
You're right, it does come off as bitter, but worse, it's not a good conversation starter. People who like Python and forced occasional Python developers tend to have different ideas about what is a "shitty syntax thing" and what is a core feature of the language.

Is there a better example of something that is generally agreed to be "shitty" yet could be fixed in a clean way, without breaking backwards compatibility?

6. mehrdadn ◴[] No.17448734[source]
Not the OP, but the inability to write

  sorted(enumerate([('a', 1), ('b', 2)]), key=lambda (i, (k, v)): (k, i, v))
in Python 3 drives me nuts. They could fix this. It worked in Python 2.
replies(3): >>17448753 #>>17448887 #>>17448994 #
7. fredley ◴[] No.17448753{3}[source]
I love writing python, but the lack of decent map/reduce/etc. (that are really good in Javascript - the other language I mainly write) hurts. Stuff like your example really feels burdensome and inelegant in comparison.
replies(1): >>17448900 #
8. tomtimtall ◴[] No.17448807[source]
Just curious, which pep would you prefer they do first? Or do you have a grudge with something not addressed in existing peps?
replies(1): >>17448959 #
9. heavenlyblue ◴[] No.17448862[source]
This comment will also come off as bitter - but why don't you just go and write your code in whatever you have been writing it in before?

Just leave it to those who have used Python for a while now and actually know what it's missing.

10. icebraining ◴[] No.17448887{3}[source]
Yeah, PEP 3113 was quite weak. It didn't even explore alternatives (like fixing the introspection without breaking the syntax).
11. pxndx ◴[] No.17448900{4}[source]
Python list comprehensions _are_ map/reduce.
replies(2): >>17448907 #>>17449257 #
12. mehrdadn ◴[] No.17448907{5}[source]
I think you mean map/filter.
replies(1): >>17453786 #
13. roel_v ◴[] No.17448928[source]
OK you're all right, I should've included some examples, here's just some from the last few days (I guess some of these are arguably not purely 'syntax', but to me they mostly come down to that, and I guess most can be explained away with 'different philosophy', and I'm sure someone will come out and say 'oh but if 'only' you had just done this and this', but still...) :

- Instantiating an object from a name in a string. Like instantiate a 'Foo' when you have a string variable that contains 'Foo'. I can't remember the syntax even though I looked it up two days ago, and I never will because it's such a shit show. Not to use PHP here as and example of a great language, but there at least the intuitive '$obj = new $var' works as you expect it. Or, in C++ you have to do it manually, which is also fine - at least be consistent.

- The weird sort-of typing of variables. Variables have types, but you can assign a different value of a different type to them, and the actual types usually doesn't matter except when it does. So you do print "Hey " + var but now you need to know what type var is because you might need to str() it.

- The whitespace-is-important-except-when-it-isn't. OK so braces are the devil's work, but when it's inconvenient, we're not that strict on white space (when initializing lists, when having expressions that span several lines, ...) so now everything can still look out of whack.

- .iteritems(). Really?

- super(ClassName, self).__init__(argument). Wut? Yes when I parse it token by token I understand, but why? Maybe the other magic methods are in this category too, but probably to a lesser degree.

- (I had some other things here about primitive OO capabilities, shitty package system/versioning, and some more, but those were all so far away from 'syntactic sugar' that they didn't fit this list no matter how hard I twisted the argument)

Look, I do understand why they are this way. For each of them, there is a reason that any reasonable person would say 'yeah that makes sense' to, possibly after some explanation of the history or context or whatever. But then at least be honest and stop promoting the language as so 'intuitive' or 'beginner-friendly' or 'much more clean than other languages'. Sure, it's not as bad as R, but it's still just like any other 20+ year old language in wide spread use - crufty, idiosyncratic in many respects, and in general requiring a bunch of frustrating head butting before you can be productive in it.

And to tie it to the OP - it seems this new syntax is promoted as being for 'beginners' or to make it 'easier to teach'. Well good luck with that, I say.

replies(2): >>17449255 #>>17465265 #
14. roel_v ◴[] No.17448959[source]
Oh but I can find grudges in anything :)

(as to your first point, if you'll allow me to be even more snarky and cynical as I already have been in this thread (might as well go all out now), the fictional 'pep' I would like to see most is 'method and apparatus to instill some realism and humility in the average Python advocate's conception and description of the language'. But here too I will freely admit that I'm probably susceptible to significant observation bias and/or bad luck, and that others could have radically different experiences from myself.)

15. msluyter ◴[] No.17448994{3}[source]
Seems that the goal here is to sort via the letters 'a', 'b', etc combined with capturing the original ordering?

You could do this, although it's admittedly uglier than your example:

  In [1]: sorted(enumerate([('b', 1), ('c', 3), ('a', 2)]), key=lambda x: (x[1][0], x[0], x[1][1]))
  Out[1]: [(2, ('a', 2)), (0, ('b', 1)), (1, ('c', 3))]
However, if you're flexible about the ordering of the resulting tuples, this seems clearer and reasonably painless:

  In [1]: sorted((x, i) for i, x in enumerate([('b', 1), ('c', 3), ('a', 2)]))
  Out[1]: [(('a', 2), 2), (('b', 1), 0), (('c', 3), 1)]
I know that doesn't address your underlying complaint. This is mainly to note that the flexibility of Python tends allow a variety of approaches and that sometimes finding the clearest one takes some effort. ("There should be one obvious way to do it..." often does not hold, IMHO.)
replies(1): >>17451148 #
16. eesmith ◴[] No.17449255[source]
"Instantiating an object from a name in a string. Like instantiate a 'Foo' when you have a string variable that contains 'Foo'. I can't remember the syntax even though I looked it up two days ago, .."

Python doesn't have specific syntax for that. It can be as simple as:

  obj = globals()["Foo"]()
That assumes "Foo" is in your global namespace. If you don't care about security then you can do:

  >>> import math
  >>> s = "math.cos"
  >>> eval(s)(3)
  -0.98999249660044542
If you care about security then you might not want to allow arbitrary objects, like "os.unlink" to be referenced. There are third-party packages which provide different models of how to get objects, like Django's "import_string" at https://docs.djangoproject.com/en/2.0/ref/utils/#django.util... .

"The weird sort-of typing of variables. Variables have types,"

Variables have only one type, "reference to Python Object". An expression like 'var = "Hey " + var' may change the type of the value that var references, just like how 'var = var * 234.567' may change the type of the value that var references from an integer/long to a float, or "var = var * "Hey"', if var == 2, causes var to be a reference to the string "HeyHey".

".iteritems(). Really"

This was for a transition phase. It no longer exists in Python 3, where items() returns an iterator instead of a list.

"super(ClassName, self).__init__(argument). Wut?"

In Python 3 this is: "super().__init__(argument)", as in:

  >>> class A:
  ...   def __init__(self, s):
  ...     print("A says", s)
  ...
  >>> class B(A):
  ...   def __init__(self, t):
  ...     super().__init__(t*2)
  ...
  >>> B("hello?")
  A says hello?hello?
  <__main__.B object at 0x10b201630>
"but it's still just like any other 20+ year old language in wide spread use - crufty, idiosyncratic in many respects"

A reason for the oft-bemoaned backwards-incompatible changes to Python 3 was to remove some of the crufty, idiosyncratic language features that you rightly pointed out. You are still using Python 2.7, so cannot take advantage of those changes.

replies(1): >>17456050 #
17. yxhuvud ◴[] No.17449257{5}[source]
Sure, but they compose really bad.
replies(1): >>17453279 #
18. zb ◴[] No.17451148{4}[source]
> this seems clearer and reasonably painless

but gives the wrong answer, as you'll see if you try to sort [('a', 2), ('a', 1)]

replies(1): >>17453601 #
19. vanderZwan ◴[] No.17453279{6}[source]
Why? Just assign to a variable, then do another list comprehension on the next line, no?
replies(1): >>17469471 #
20. msluyter ◴[] No.17453601{5}[source]
Sure; it wasn't clear to me whether the letter fields could be repeated.
replies(1): >>17459132 #
21. pxndx ◴[] No.17453786{6}[source]
Yes, thanks for correcting me.
22. roel_v ◴[] No.17456050{3}[source]
Ok the 'global' syntax thing makes a lot more sense than the one I was told about (which involved the 'inspect' module iirc). The typing - yes I understand all of that, my point still stands that it's all 'you don't need to know all of this! Oh, at least, until you do'. Wrt python3 - fair enough, things probably do get better. But that just reinforces my point about cruft building up in any practical language, and Python being just as susceptible to it as other languages.
23. mehrdadn ◴[] No.17459132{6}[source]
Why would I have included the indices for sorting if the letters were guaranteed to be distinct...
24. duckerude ◴[] No.17465265[source]
Referring to a class by its name is the wrong approach in Python, because classes are themselves objects.

In PHP, a class is uniquely identified by its name, everywhere. If you define a class Foo, then new $var will resolve correctly either everywhere or nowhere (the name needs to be fully qualified, to avoid namespace headaches).

That's not the case in Python. A class Foo has the same status as any other object. That means you can't rely on its name - it could be replaced by another value. But it does mean you can pass the class itself around instead of its name. Instead of putting the string 'Foo' or the literal Foo::class into a data structure or an argument list, you can just put Foo in there, and call it later.

I think the Python approach is cleaner, but then again, it's what I already knew when I first learned how PHP did it.

Python doesn't need to instantiate classes based on string values, so it doesn't provide an easy way to do that.

Python almost allowing you to take the PHP approach is a bit of a pattern. Python is dynamic enough to let you do a lot of things you shouldn't be doing. Ugly syntax like globals()[var] is usually (but not always) a sign that you're looking for the wrong kind of solution.

25. yxhuvud ◴[] No.17469471{7}[source]
And that is a pretty sad state of affairs compared to ways that do compose well, like the one in Ruby which you can easily chain.