From inside a loop, nested within another loop [etc], returning to the base code block.
Python |
Qt/Cpp |
- # Special thanks from jinsuun__ on #python irc
- from dataclasses import dataclass
- @dataclass
- class C:
- m_c: int
- isLetter : bool = False
- isNonCharacter : bool = False
- isNull : bool = False
- isNumber : bool = False
- isPrintable : bool = True
- isSpace : bool = False
- isUpper : bool = False
- n = 88
- c = C(n)
- match c:
- case C(0):
- c.isNull = True
- case C( m_c = x ) if x in range( 1, 32 ) or x == 127:
- c.isNonCharacter = True
- c.isPrintable = False
- case C(" "):
- c.isSpace = True
- case C(x) if x in range( ord("A"), ord("Z") + 1 ):
- c.isUpper = True
- c.isLetter = True
- case C(x) if x in range( ord("a"), ord("z") + 1 ):
- c.isUpper = False
- c.isLetter = True
- case C(x) if x in range( ord("0"), ord("9") + 1 ):
- c.isNumber = True
- case _:
- pass
|
- qint8 c(88);
- bool isLetter (false);
- bool isNonCharacter(false);
- bool isNull (false);
- bool isNumber (false);
- bool isPrintable (true );
- bool isSpace (false);
- bool isUpper (false);
- switch (c) {
- default: break;
- case 0 :
- isNull = true;
- [[fallthrough]];
- case 1 ... 31:
- case 127 : // Delete
- isNonCharacter = true;
- isPrintable = false;
- break;
- case ' ':
- isSpace = true;
- break;
- case 'A' ... 'Z':
- isUpper = true;
- [[fallthrough]];
- case 'a' ... 'z':
- isLetter = true;
- break;
- case '0' ... '9':
- isNumber = true;
- break;
- }
|
Read as:
|
- if [ expression is true ]
- print x y z
- 'break'out of loop z [ ignore the else ]
- 'break'out of loop y [ ignore the else ]
- 'break'out of loop x.
|
- if [ expression is true ]
- debug out x y z
- and goto finished [ label ]
|
Benefits:
|
- In python, you can place an "else" statement after a loop ( "for" or "while" ) which gets called if the loop exits without a 'break' statement. This is a novell construct absent in c++."
|
- This code, is clean, and in this particular case, no extra indentation was needed.
- the "goto" label is particularly descriptive: "[ I am finished ] goto finish line."
|
Drawbacks:
|
- This "else" feature is esoteric and unknown towards most Python developers.
- The "else" mistakenly seems like the continuation to the "if" statement above.
- This feature does not exist in any other language, making this a non-transferable idiom.
- Indentation structure is bumpy and disorienting.
|
|