3 ms·
> Compared to Python for example where you'd literally just pass the array to the method like method(array). How does one tell Python to pass the contents of s
by mfontani 6y ago
> Compared to Python for example where you'd literally just pass the array to the method like method(array).
How does one tell Python to pass the contents of said array as distinct parameters to the function, instead of as a lone array parameter?
In Perl, that's the difference between foo(@bar) and foo(\@bar) or foo(1, 2, 3) vs foo([1, 2, 3]).
- cesarb 6y ago> How does one tell Python to pass the contents of said array as distinct parameters to the function, instead of as a lone array parameter? With an asterisk: method(*array)
- mfontani 6y agoRight, which I sorta kinda get why coming from C - a *something is the thing pointed to by that something. How is that more understandable than @ vs \@ without knowing the language? My guess is it isn't.
- rjeli 6y agoAs someone who learned python first - the idea that `myfn(@xs)` would call `myfn(1,2,3)` is... unnerving. Implicitly splicing in arguments makes me wonder what other kind of syntactic oddities are going over my head in my code.
- joshuamorton 6y agoPython is "relatively" consistent about * and being used to expand or contract arrays and dicts, respectively. a, *, b = [1,2,3,4] def foo(*args): bar(*[1,2,3]) all do "what you'd expect" from the single concept that "* is sort of pattern-matchy for an array". But the more important bit is just that you don't have to prefix variables with @ and $. Python is optimized for writing code that acts on variables, while perl is optimized for code that acts on strings. While strings are certainly a common data type, most code isn't modifying strings directly. So optimizing for that case doesn't make a lot of sense.
- kansface 6y agoa = [1] def f(b): print(b) f(a) f(*a)