Python is readable

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Mar 16 13:01:28 EDT 2012


> >> I don't understand the reason for $arg1 and $arg2. Is there some reason
> >> why the code couldn't do this instead?
> >>
> >>         my(@list1, @list2) = @_;
> >
> > @_ contains references to arrays. You can't pass two arrays to a
> > function.
> 
> 
> Why ever not? That seems like basic functionality to me. I can't imagine
> any modern language that lacks such a simple feature. Even Pascal allows
> you to pass arrays as arguments to functions.
> 
> Is there some design principle that I'm missing that explains why Perl
> lacks this feature?


My understanding is that it assigns each scalar argument until it finds a list
to assign and then it assigns everything remaining to the list.

my @arr = ( 'test', 'blah', '1234', 'boop', 'foo', 'bar' );
my @arr2 = ( 'adsf', 'qwerty' );
print "@arr\n";
my @arr3 = (@arr, @arr2);
print "arr3:@arr3\n";
my ($arg1, $arg2, @arg3) = @arr3;
print "arg3:@arg3\n";

bash-3.2$ perl temp.pl
testblah1234boopfoobar
arr3:test blah 1234 boop foo bar adsf qwerty
arg3:1234 boop foo bar adsf qwerty


I assume this is because it combines both elements of the list into one 
giant list and then if you try and assign two lists it does not know
where to split it. Now if you pass a reference to the two arrays instead 
of the values it should work as expected, but now you are dealing with pointers / 
references.


bash-3.2$ cat temp.pl
my @arr = ( 'test', 'blah', '1234', 'boop', 'foo', 'bar' );
my @arr2 = ( 'adsf', 'qwerty' );
print "@arr\n";
my @arr3 = (\@arr, \@arr2);
print "arr3:@arr3\n";
my ($arg1, $arg2, @arg3) = @arr3;
print "arg1:@$arg1\narg2:@$arg2\narg3:@arg3\n";

bash-3.2$ perl temp.pl
test blah 1234 boop foo bar
arr3:ARRAY(0xb2f0f90) ARRAY(0xb2f1020)
arg1:test blah 1234 boop foo bar
arg2:adsf qwerty
arg3:





Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list