assigning values in python and perl

J. Peng peng.kyo at gmail.com
Wed Jan 16 22:40:59 EST 2008


May I ask, python's pass-by-reference is passing the object's
reference to functions, but perl, or C's pass-by-reference is passing
the variable itself's reference to functions. So althought they're all
called pass-by-reference,but will get different results.Is it?

On Jan 17, 2008 11:34 AM, J. Peng <peng.kyo at gmail.com> wrote:
> I just thought python's way of assigning value to a variable is really
> different to other language like C,perl. :)
>
> Below two ways (python and perl) are called "pass by reference", but
> they get different results.
> Yes I'm reading 'Core python programming', I know what happened, but
> just a little confused about it.
>
> $ 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);
> }
>
> my @a = (1,2,3);
> test(\@a);
>
> print "@a";
>
> $ perl t1.pl
> 4 5 6
>



More information about the Python-list mailing list