4 ms·
I don't get why they discarded for/while... as label1: __for/while... as label2: ___break label1 suggestion. It actually seems a good enough idea to impleme
by norhi999 4y ago
I don't get why they discarded
for/while... as label1:
__for/while... as label2:
___break label1
suggestion. It actually seems a good enough idea to implement that. And it's rather concise. I often need to break deep inner loops to outermost and doing it with flags is... strange.
- hansvm 4y agoMildly on-topic, Zig has basically that behavior, and it's really smooth in my experience.
- eesmith 4y agoCould you give a real-world example of how it proved useful? Eg, quoting the text: > Angelico noted that he has used the Pike programming language, which does have a labeled break. He found that he had used the feature twice in all of the Pike code he has written. Neither of the uses was particularly compelling in his opinion; one was in a quick-and-dirty script and the other is in need of refactoring if he were still working on that project, he said. That was essentially all of the real-world code that appeared in the discussion.
- IshKebab 4y agoRust supports this. It appears to be used more than twice... https://grep.app/search?q=break%20%27%5Cw®exp=true&case=true&filter[lang][0]=Rust https://grep.app/search?q=break%20%27%5Cw®exp=true&case=t...
- eesmith 4y agoMy example was "twice by one developer", not "twice across all indexed repos." A spot check shows that quite a few in your link are used specifically to ensure correct handling of Rust multi-level breaks work syntax, like https://github.com/rust-lang/rust-analyzer/blob/master/crates/ide/src/highlight_related.rs#L746 https://github.com/rust-lang/rust-analyzer/blob/master/crate... , https://github.com/rust-lang/rustfmt/blob/master/tests/source/label_break.rs https://github.com/rust-lang/rustfmt/blob/master/tests/sourc... , https://github.com/rust-lang/rust/blob/master/src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_label_to_loop.rs https://github.com/rust-lang/rust/blob/master/src/tools/rust... , https://github.com/rust-lang/rust/blob/master/src/tools/rust-analyzer/crates/hir-ty/src/tests/simple.rs https://github.com/rust-lang/rust/blob/master/src/tools/rust... and likely more. Another is a translation of BASIC code to Rust, using break as a form of goto. https://github.com/coding-horror/basic-computer-games/blob/e93a709930bfed713a06fc8c240447c05e0a2645/65_Nim/rust/src/main.rs#L170 https://github.com/coding-horror/basic-computer-games/blob/e... . The Python version at https://github.com/coding-horror/basic-computer-games/blob/e93a709930bfed713a06fc8c240447c05e0a2645/65_Nim/python/Traditional_NIM.py https://github.com/coding-horror/basic-computer-games/blob/e... doesn't use that approach. The example at https://github.com/tokio-rs/mio/blob/master/tests/tcp.rs https://github.com/tokio-rs/mio/blob/master/tests/tcp.rs is a nice one // Wait for our TCP stream to connect 'outer: loop { poll.poll(&mut events, None).unwrap(); for event in events.iter() { if event.token() == Token(1) && event.is_writable() { break 'outer; } } } though it can be replaced with a helper-function (note: I don't fully know what the code is doing, but the following looks right): def find_writable_event(events): for event in events: if event.token() == Token(1) and event.is_writable(): return True while 1: poll.poll(events, None) if find_writable_event(events): break So while there's likely a good real-world example of something which can't easily be re-written in an alternative form for Python, simply pointing to a grep result isn't all that persuasive.
- IshKebab 4y agoSure, but I don't think you can really evaluate a feature based on how many times one developer uses them. Nor even on how many times they are used in total. There are a ton of Python features (and misfeatures) that I'm sure most Python devs never use. __subclasses__ for example, or sitecustomize.py. Similarly I'm pretty sure Rust's i128 is almost never used but it would be really weird to omit it.
- eesmith 4y agoWhat they can evaluate is that no one has yet come up with a real-world example where it's a useful addition to Python, and at least some of those people involved in the discussion have experience with multi-level break from other languages. Hence my "Could you give a real-world example of how it proved useful?" Convincing examples might change their minds! While simply saying "here's how to find Rust programs which use that construct" is not informative nor convincing. With __subclasses__ there are definite real-world examples where they are useful. We can see the motivation in the commit history: commit 1c45073aba8f4097b4dc74d4fd3b2f5ed5e5ea9b Author: Guido van Rossum <guido@python.org> Date: Mon Oct 8 15:18:27 2001 +0000 Keep track of a type's subclasses (subtypes), in tp_subclasses, which is a list of weak references to types (new-style classes). Make this accessible to Python as the function __subclasses__ which returns a list of types -- we don't want Python programmers to be able to manipulate the raw list. In order to make this possible, I also had to add weak reference support to type objects. This will eventually be used together with a trap on attribute assignment for dynamic classes for a major speed-up without losing the dynamic properties of types: when a __foo__ method is added to a class, the class and all its subclasses will get an appropriate tp_foo slot function. and the commit logs show an example of use: Author: Victor Stinner <victor.stinner@gmail.com> Date: Fri Mar 25 17:36:33 2016 +0100 ... * Use __subclasses__() to get resource classes instead of using an hardcoded list (2 shutil resources were missinged in the list!) ... In addition, we can find third-party packages which use it. I've never used 128-bit integers, but its seems many people do have real-world cases for it. What do you think is a convincing real-world example that should motivate its inclusion in Python?
- shusaku 4y agogoto label1
- benj111 4y agoI can see circumstances where you've just got one loop, it would be easier to follow.
- montroser 4y agoAgreed -- Perl does it with labels this way, and JavaScript too. It has always seemed totally sensible in the occasional real-world examples I've seen in the wild.