4 ms·
> 10 different ways of doing the same thing Mostly an urban myth. Where it isn't, at least 8 of those ways are actually important. Exercise: write a python p
by bloobloobloo 9y ago
> 10 different ways of doing the same thing
Mostly an urban myth. Where it isn't, at least 8 of those ways are actually important.
Exercise: write a python program that prints Python's quoting documentation, without external files, without editing the documentation. Spoiler alert: it's impossible.
- gjm11 9y agoI'm not sure I understand your exercise. What do you mean by "Python's quoting documentation", and what do you mean by "editing" it?
- yorwba 9y agoI was wondering about that, too. Unless `"Python's quoting documentation"` is an easter-egg in "help()" that recursively contains a quoted version of itself, it should be possible to just escape all problematic characters. But maybe that counts as `"editing"`? Not sure.
- bloobloobloo 9y ago> maybe that counts as `"editing"` how would it not
- bloobloobloo 9y ago> What do you mean by "Python's quoting documentation" String literals are described by the following lexical definitions: stringliteral: shortstring | longstring shortstring: "'" shortstringitem* "'" | '"' shortstringitem* '"' longstring: "'''" longstringitem* "'''" | '"""' longstringitem* '"""' shortstringitem: shortstringchar | escapeseq longstringitem: longstringchar | escapeseq shortstringchar: <any ASCII character except "\" or newline or the quote> longstringchar: <any ASCII character except "\"> escapeseq: "\" <any ASCII character> You can't paste that text into a file and write a Python program around it that outputs the same text. You need to encode the quote characters. More generally, there is no way in (many languages including) Python to treat an arbitrary (yet known) block of data as data.
- b2gills 9y agoFor those that don't know you can use `「` and `」` to quote anything that does not contain `」` and Perl 6 won't process it in any way other than storing it as a Str. If you needed to have `」` in there you can fall back to `Q{{{{{{…}}}}}}` or `Q:to<END>;…END`
- dmytrish 9y agoSo, how exactly these different ways of getting sub arguments are useful? sub add1 { my ($arg1, $arg2) = @_; $arg1 + $arg2; } sub add2 { my $arg1 = shift; # possibly two screens of dense code here, then my $arg2 = shift; $arg1 + $arg2; } sub add3 { $_[0] + $_[1] } # There may be some other ways to extract arguments # that I am not aware of. # It's possible to combine any of the methods! And this is just argument access, one of the core language primitives.
- kbenson 9y ago> So, how exactly these different ways of getting sub arguments are useful? Because in the second (add2), you might do something interesting with the second (or third) arguments depending on the first. You might have optional arguments. Your arguments might be a list of items you don't know the size of. It allows you to develop what you need out of the extensible core mechanism. Modules can and have built on that. > # There may be some other ways to extract arguments There are. Now you can just do sub add( $arg1, $arg2 ) { $arg1 + $arg2; } My personal favorite is to use Function::Parameters[1] fun add( Num $arg1, Num $arg2 ) { $arg1 + $arg2; } which allows named params, type constraints (runtime), default params, etc. 1: https://metacpan.org/pod/Function::Parameters https://metacpan.org/pod/Function::Parameters
- dmytrish 9y agoI cannot imagine any way of practical (ab)using this flexibility that could not be covered in Python with its only way of handling arguments.
- kbp 9y agoYou aren't really showing off different ways to get arguments in any of those examples. In all of them, you get the arguments in a list called @_; you're just showing off three different ways to get values out of a list, which Python has plenty of ways to do as well. Translating your examples into Python: def add1(*_): _ = list(_) arg1, arg2 = _ return arg1 + arg2 def add2(*_): _ = list(_) arg1 = _.pop(0) arg2 = _.pop(0) return arg1 + arg2 def add3(*_): _ = list(_) return _[0] + _[1]