3 ms·
"Python, Ruby, JavaScript and many other dynamic languages do not make the programmer think about whether you are going to pass a data structure like an array o
by pmoriarty 7y ago
"Python, Ruby, JavaScript and many other dynamic languages do not make the programmer think about whether you are going to pass a data structure like an array or hash as a reference or direct value. Perl does."
If you don't think about these things in python, you're going to be scratching your head when you see something like this:
>>> foo = ['a','b','c']
>>> bar = foo
>>> bar[1] = 'z'
>>> foo
['a', 'z', 'c']
- ben509 7y agoIt's incredibly confusing if you naively use a list as a default argument, e.g. def foo(some_list=[]): ... some_list.append(3) ... Since the default is constructed when the function is evaluated, it's the same list, but only when the default is used. Or the n00b's attempt to use lambdas: def list_of_lambdas(x): result = [] for n in range(x): result.append(lambda m: n + m) return result for func in list_of_lambdas(3): print(func(3)) 5 5 5 (All the closures share the same "cell", meaning the same "n", which is 2 at the end of that loop. Worse, if you 'yield lambda ...', that example would look like it worked. So, yes, Python absolutely has references.)
- jlokier 7y agoYes indeed, that's a bug-prone misfeature of Python. Perl and JavaScript both get default arguments right: They make a new array on each call. Perl and JavaScript also get the for-loop closures right as well. In Perl, "for (my $n = ...)" and in JavaScript "for (let n = ...)" will both create a new "cell" each time around the loop, so closures work as expected. (However, MSIE's version of "let" doesn't create a new "cell" each time, and this can be a source of difficult to see event callback bugs; best transpiled out.)
- zbentley 7y agoPerl has default arguments (outside of Devel::Declare trickery)?
- jlokier 7y agoYes, built-in since Perl 5.20, released mid 2014. use experimental qw(signatures); sub plus_one($x = 100) { return $x + 1; } # Says 6. say plus_one(5); # Says 101. say plus_one(); (The subroutine signatures feature is still marked experimental, which is a shame. I use it in virtually all my code, unless I want pre-5.20 compatibility.)
- zbentley 7y agoNeat, thanks! I can't use it unfortunately (the little Perl I still write targets 5.8 on ancient CentOS, but that's not the language's fault). Good to see it still evolving!
- tasty_freeze 7y agoNo, it isn't the same. I'm no language lawyer, but this is my understanding of it. In python, $foo is a reference. It might be a reference to a scalar value, or list, or hash, etc, but it is a reference. The code you wrote might cause confusion for people who don't understand the python model. In perl, $foo might be a scalar value, or it might be a reference to a value of some kind. Perl has this exact same type of problem demonstrated by your python code, but in addition it has the problem of "is $foo a scalar value or a reference?"
- pflanze 7y agoI think OP's point was that even in Python you have to be conscious where your "storage" is--that you're sharing it. Yes, the "Python" model (or Java, JavaScript) of referring to everything by reference is uniform and simple--except it still shares the issue of having to care if you add mutation into the mix. Due to its apparent smoothness it might even make it easier to run into that. In a sense Perl is honest here in saying I use local arrays via @ prefix and I know this array "lives here", like in the C/C++ world where you have to care about where the storage is so you don't pass references to stack objects etc.; in this world, when you take a reference, it's a heads-up that you may have to care about something. In Perl, unlike in C/C++, you don't have to care about memory safety, but you still, like in all imperative languages, you have to care about sharing of mutations. An explicit reference makes this explicit. If you return an array flat (without taking a reference) in Perl, it is being copied, and while that's slow, it is at least safe. It's an unclean solution for the issue, but in a sense it's at least pointing your head towards the issue. The clean solution for this is functional programming. Lisp was first to use a uniform "everything is a reference, implicitly" model. But it also preferred a functional programming model where you don't mutate your data structures. Java and Python took the reference model but mixed it with an imperative data update model. Meh. And I agree that I like the "just use references for everything implicitly" approach. But I also like to combine it with a functional one. And I ended up creating a project to achieve exactly that in Perl, and will shamelessly plug it here: https://metacpan.org/pod/FunctionalPerl https://metacpan.org/pod/FunctionalPerl (Code written in it co-exists with code that still uses non-reference variables, so feel free to argue that "now you have both worlds mixed", but for one it aims at existing Perl programmers who know how to deal with the non-reference model, and secondly functional programming matters most in the upper levels of an application; it's fine to use imperative code in inner loops / enclosed in an otherwise pure function, and it's fine to use array/hash variables there; FunctionalPerl comes in where you'd traditionally take a reference via `\`; so it's kind of a split between imperative and functional world now).