"always passes by reference"

Zenin zenin at rhps.org
Mon Aug 14 21:56:13 EDT 2000


(Greg Weeks) <weeks at golden.dtc.hp.com> wrote:
: (Greg Weeks) (weeks at golden.dtc.hp.com) wrote:
: : I prefer the old way of speaking.  With the new way of speaking, both
: : Python and Perl are "pass by reference", but Perl is somehow *more* so.
: 
: If it isn't clear what I mean, here's some Perl:
:     sub f
:     {
:         $_[0] = 42;
:     }
:     $x = 1;
:     f($x);
:     print "$x\n";		# PRINTS 42
: and the corresponding Python:
:     def f(a):
: 	a = 42
:     x = 1
:     f(x)
:     print x			# PRINTS 1
: 
: Good Python!  Bad Perl!

	No, it's not.  The problem here isn't Python or Perl, it's your lack
	of understanding.  That is to say your two examples are not the
	same (which should be obvious, as the results are different).  The
	Perl example should really be:

		sub f {
		    my $a = shift;
		    $a = 42;
		}
		$x = 1;
		f($x);
		print $x;  # PRINTS 1

	Perl gives you easy access to modify the call stack if you require
	it, but as always just because you *can* do something doesn't mean
	you *should*.  Perl simply gives the user more power here...which
	granted also means it requires greater responsibility on the part of
	the user.  After all, Perl isn't Newspeak; that's Python's job. :-)

-- 
-Zenin (zenin at rhps.org)                   From The Blue Camel we learn:
BSD:  A psychoactive drug, popular in the 80s, probably developed at UC
Berkeley or thereabouts.  Similar in many ways to the prescription-only
medication called "System V", but infinitely more useful. (Or, at least,
more fun.)  The full chemical name is "Berkeley Standard Distribution".



More information about the Python-list mailing list