assigning values in python and perl

Hrvoje Niksic hniksic at xemacs.org
Thu Jan 17 07:55:29 CET 2008


"J. Peng" <peng.kyo at gmail.com> writes:

> $ cat t1.py
> def test(x):
>     x = [4,5,6]
>
> a=[1,2,3]
> test(a)
> print a
>
> $ python t1.py
> [1, 2, 3]
>
> $ cat t1.pl
> sub test {
>     my $ref = shift;
>     @$ref = (4,5,6);
> }

@$ref = (4, 5, 6) intentionally assigns to the same list pointed to by
the reference.  That would be spelled as x[:] = [4, 5, 6] in Python.
What Python does in your example is assign the same as Perl's $ref =
[4, 5, 6].  So they're not so different after all.



More information about the Python-list mailing list