9 ms·
Literally the second item in the "Zen of Python" (https://www.python.org/dev/peps/pep-0020/ https://www.python.org/dev/peps/pep-0020/): Explicit is better than
by usrbinbash 5y ago
Literally the second item in the "Zen of Python" (https://www.python.org/dev/peps/pep-0020/ https://www.python.org/dev/peps/pep-0020/):
Explicit is better than implicit.
And yet, s = ["one", "two" "three"] will implicitly and silently do something, that is probably wrong most of the time.
- rat9988 5y agoThis is not what implicit is about.
- ianbicking 5y agoImplicit concatenation sure seems implicit to me
- jstx1 5y agoI mean the zen being wrong is kind of a meme at this point. The whole “only one obvious way to do it” isn’t just false but the exact opposite is true. Python is one of the most flexible languages with many many ways to do the same thing; more than any other language I can think of.
- fault1 5y agothe zen of python was written in the 90s. from that context it makes sense, because the only goal of python in the 1990s was to be more popular than perl, which was notorious in having many ways of doing the same thing. but yeah, python had had significant feature creep over the years, it's nowhere near the small clear lang it used to be.
- deleted 5y ago[deleted]
- andi999 5y agoAnd still no expressive switch/case statement, breaking out of loops and ending scripts early (for explorative programming).
- jstx1 5y ago> And still no expressive switch/case statement There's match/case in 3.10 - https://www.python.org/dev/peps/pep-0636/ https://www.python.org/dev/peps/pep-0636/
- gabagool 5y ago>no expressive switch/case statement match/case (not a drop in switch statement) >breaking out of loops break >ending scripts early (for explorative programming) exit() or sys.exit()
- solox3 5y agoNotice that, in the original quote, There should be one-- and preferably only one --obvious way to do it. the author used two different ways of hyphenating (three, if you count the whole PEP 20). PEP 20 is clearly not meant to be taken as law. Nor PEP 8. Nor PEP 257. People frequently mistake "one obvious way" with "one way". There are lots of ways to iterate through something, for example, but there is really one obvious way. And the philosophy here still applies: when you read anyone else's python code, the obvious way is probably doing the obvious thing. I think that is the more appropriate takeaway from PEP 20.
- xigoi 5y agoI can think of at least 2 obvious ways to iterate through something: for loops and comprehensions.
- voltagedivider 5y agoYou're right that both iterate through something but `for` loops and comprehensions aren't used as if they were interchangeable. For example, you'll sometimes see people do bad stuff like this: >>> lst = [] >>> >>> [lst.append(i + i) for i in range(10)] [None, None, None, None, None, None, None, None, None, None] >>> >>> lst [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] >>> When they should be doing this: >>> lst = [] >>> >>> for i in range(10): ... lst.append(i + i) ... >>> lst [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] >>> Or just this: >>> lst = [i + i for i in range(10)] >>> >>> lst [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] >>>
- oblvious-earth 5y agoIt was a meme when Zen was written, the spaces around the em dash are handled 3 different ways. Twice in the line you abbreviated, removing the joke.
- savant_penguin 5y agoMatplotlib is an example of a library with at least two "correct" ways of plotting
- jstx1 5y agoBut only one of them is recommended - the one that makes less sense.
- jofer 5y agoHow is working with figure and axes objects the one that makes less sense? Is it really that crazy do set up a figure, axes on that figure, and plot on the axes, returning an artist object for each plotting command?
- jstx1 5y agoYes, it is crazy. I guess this isn't really the place for it but ... From the official docs: The Figure is the final image that may contain 1 or more Axes. The Axes represent an individual plot (don't confuse this with the word "axis", which refers to the x/y axis of a plot). This is infuriatingly bad and I firmly believe that it makes sense only to people who already know how it works. There's an image, axes (this word alone is a crime), plot, figure... it's like they took a bunch of synonyms and arranged them randomly to put together an API.
- dylan604 5y ago>axes (this word alone is a crime), why so? you prefer something like axiis?
- jstx1 5y agoSee, that's the thing: > Axes object is the region of the image with the data space. In matplotlib axes is not the plural of axis. It has its own meaning specific to the API. And at the same time it's the plural form of another word (axis) which is also relevant in this context and it sounds almost identical when pronounced.
- Quekid5 5y agoIt's sort of like the Unix Philosophy. It sounds good and is probably a good thing to strive for generally, but it's ultimately pointless when it comes to actually evaluating whether approach A is better than approach B.
- webmaven 5y ago> I mean the zen being wrong is kind of a meme at this point. The whole “only one obvious way to do it” isn’t just false but the exact opposite is true. Python is one of the most flexible languages with many many ways to do the same thing; more than any other language I can think of. Not in comparison to Perl, which usually has multiple ways to do anything, each 'obvious' to different sets of people (each Perl codebase therefore seems to have a distinct dialect based on which 'obvious' alternatives are chosen). The other direction languages can take that is being contrasted, is there being one non-obvious way to do something. Python's 'most obvious way' isn't necessarily the fastest/most concise/most efficient/scalable/etc. way to do something in Python, but it will usually be obvious to most Python developers. And although broad styles have certainly developed over time (imperative, functional, OO) as Python has gained power and flexibility, the dictum still largely holds true.
- hnlmorg 5y ago10 years ago I'd have agreed with you. But Perl has gone a long way in pulling back from some of that insanity while Python has been giving C++ a run for it's money in terms of features.
- onphonenow 5y agoI'd totally agree - there's been a burst of sort of the perl style stuff (:= ?) to gain relatively small wins. ie, instead of for line in lines: print(line) we are supposed to be using while line := f.readline(): print(line) I've not been super impressed with this type of thing. That said, string formatting is better with f strings. They also rolled back some the forced breakage from trying to force unicode with 3 which made a big difference. 3.3 added back u'' Lots of good cleanups lstrip vs removeprefix etc. Underscores in numeric literals (10000000 vs 10_000_000) So lots of good stuff still landing.
- dragonwriter 5y ago> ie, instead of > for line in lines: print(line) > we are supposed to be using > while line := f.readline(): print(line) No, we’re not. Walrus, in loops, IME, is more for replacing this pattern: while True: myvar = get_it() if not ok(myvar): break # code that uses myvar with this pattern: while ok(myvar := get-it()): # code that uses myvar
- egeozcan 5y ago> Complex is better than complicated What? Something being complex is artificial, we try to avoid it. Problems can be complicated, we try to simplify them, and more complicated the problem is, we tend to develop more complex solutions. So comparing them does not make sense? Or did I always know them wrong?
- stonemetal12 5y agoComplex: consisting of many different and connected parts. Complicated: consisting of many interconnecting parts or elements; intricate. Nothing specifically artificial about either one. Software that is well decomposed is Complex (made of many smaller connected parts). Software that is is poorly decomposed is Complicated (made of many smaller interconnected parts). Connected vs interconnected? Interconnected: connected at multiple points or levels (aka spaghetti code)
- nighthawk454 5y agoIt's not particularly well-worded. A lot of dictionaries list complex/complicated as synonyms. I always took it to mean 'complex' as in having many connected parts, and 'complicated' more as in over-complicated or convoluted - the opposite of 'simple'. In other words, breaking something complicated into a system of intentionally-designed pieces is probably better than a chunk of opaque code to brute-force the current case. A good system is probably also 'simpler', despite having more pieces and interconnects.
- hibrass 5y ago
- pmarreck 5y agoExcept exit. I knew Python wasn't for me in my first foray into it when I fired its REPL and then went to exit it with control-C or whatever and it literally printed out the right way to do it but then didn't do it. Python was more interested in having me do things a certain way even when it knew what I intended to do, just to be a twit.
- animal_spirits 5y agoCtrl-c raises a KeyboardInterrupt error, which is useful for programs to catch. If you type >>> exit Use exit() or Ctrl-D (i.e. EOF) to exit You will get that error response. The goal of this is to have the REPL language the exact same as the scripting language. exit() is supposed to be called as a function to make the language more consistent, so just typing `exit` will do nothing
- usrbinbash 5y ago> which is useful for programs to catch. Useful would be, if the default handler for SIGINT would not raise an exception, but have a useful default like eg. terminating the program. Go handles SIGINT this way by default. If I want an exception, I can just tell the program: import signal signal.signal(signal.SIGINT, throwException()) The way it is now, the exception bubbles up to runtime, and if it isn't handled (eg. in the REPL) the program crashes, or worse, hangs if there are other threads of execution running: import threading import time def sleepN(): for i in range(20): time.sleep(1) threading.Thread(target=sleepN).start() time.sleep(20) Press c-C here, and the thread will still run, because the bubbled up Excp only kills the main thread. This is a real footgun in applications which rely on SIGINT being a termination signal, and have long running threads.
- jrockway 5y agoThe REPL prints the value of a variable that you type in. exit is a variable, and so the REPL prints its value. If you want to run it as a function, you can do that, and indeed its string value is a message telling you to do that. $ python3 Python 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> exit Use exit() or Ctrl-D (i.e. EOF) to exit >>> exit.eof 'Ctrl-D (i.e. EOF)' >>> exit.name 'exit' >>> exit = 42 >>> exit 42 >>> exit() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not callable >>> I would have special-cased exit, though.
- lenkite 5y agoPython finally ended up following Perl's TMTOWTDI motto! https://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it https://en.wikipedia.org/wiki/There%27s_more_than_one_way_to...
- dpedu 5y agoHmmm, it sounds like you're expecting "two" and "three" to be separate list elements because of some sort of implicit behavior due to being written in a list context. This is the opposite of what "Explicit is better than implicit" means. This is a list and you must explicitly place a comma when you want to start a new element in the list. Is there ever a time a new element follows a previous one and is NOT separated by a comma? No, this is explicit. Whereas, strings also always concatenate in this manner be it in a list context or not. It seems like you're assuming behaviors from other languages would be the same in another.
- doubleunplussed 5y agoYour sarcasm is misplaced. I would prefer a SyntaxError to either of the implicit behaviours.
- kazinator 5y ago> Is there ever a time a new element follows a previous one and is NOT separated by a comma? Yes: [ "one, two", "three" ] The comma is not an absolute context-free indicator of element separation.
- matsemann 5y agoNo, we don't want it to implicitly be a list item. We want it to fail as invalid syntax. If I wanted the two and three strings to be combined, I would have /explicitly/ used an operator for that. It's the implicit behavior of that which is the problem.
- fantod 5y agoNot to mention the implicit string concatenation that you get instead.
- ReleaseCandidat 5y ago> it sounds like you're expecting "two" and "three" to be separate list elements I'd expect that to be an error.
- deleted 5y ago[deleted]