![]() |
Are braces rather than indentation ever wanted? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Are braces rather than indentation ever wanted? (/thread-3128.html) |
RE: Are braces rather than indentation ever wanted? - nilamo - May-02-2017 (May-01-2017, 03:38 PM)Ofnuts Wrote: don't use switch/case statementsThat's funny, I just recently watched a video about HolyC's switch statement. https://www.youtube.com/watch?v=tBQ69ZnuMbc (nsfw... just like everything related to TempleOS) The short version is that he added start/end clauses to switch statements, to add additional pattern matching for groups of cases. Additionally, it's worth noting that in HolyC, if you don't set a variable, it's sent to stdout, so each branch of the switch is echoing output (that might just be for strings, or single quoted strings, I'm not sure). I64 i; for (i=0;i<10;i++) { switch (i) { case 0: 'Zero\n'; break; case 1: 'One\n'; break; case 2: 'Two\n'; break; start: '['; case 3: 'Three'; break; case 4: 'Four'; break; case 5: 'Five'; break; case 6: 'Six'; break; end: ']\n'; break; case 7: 'Seven\n'; break; case 8: 'Eight\n'; break; case 9: 'Nine\n'; break; } }Which is roughly the same as: def wrapped(num): return '[{0}]'.format(num) switch = { 0: 'Zero', 1: 'One', 2: 'Two', 3: lambda: wrapped('Three'), 4: lambda: wrapped('Four'), 5: lambda: wrapped('Five'), 6: lambda: wrapped('Six'), 7: 'Seven', 8: 'Eight', 9: 'Nine' } for i in range(10): val = switch[i] # the same as a `if` in this example while callable(val): val = val() print(val)
RE: Are braces rather than indentation ever wanted? - Mekire - May-02-2017 >>> from __future__ import braces File "<stdin>", line 1 SyntaxError: not a chanceReally. Try it yourself. RE: Are braces rather than indentation ever wanted? - volcano63 - May-02-2017 (May-02-2017, 05:49 AM)Mekire Wrote:Someone has a good taste - and a nice sense of humor>>> from __future__ import braces File "<stdin>", line 1 SyntaxError: not a chanceReally. Try it yourself. ![]() ![]() |