How to best explain a "subtle" difference between Python and Perl ?
David C. Ullrich
dullrich at sprynet.com
Tue Aug 12 12:27:26 EDT 2008
In article
<4290ea3f-43b9-455d-bd60-587369842c5b at s50g2000hsb.googlegroups.com>,
Palindrom <demarcheb at gmail.com> wrote:
> Hi everyone !
>
> I'd like to apologize in advance for my bad english, it's not my
> mother tongue...
>
> My girlfriend (who is a newbie in Python, but knows Perl quite well)
> asked me this morning why the following code snippets didn't give the
> same result :
>
> ### Python ###
>
> liste = [1,2,3]
>
> def foo( my_list ):
Right now my_list is a local variable - since you
called foo(liste), the name my_list is bound to the
object liste
> my_list = []
But this line doesn't modify liste, instead it
binds the name my_list to a different object.
If you want to modify the list that my_List points
to you could do it in either of two ways:
def foo1(my_liste):
del my_liste[:]
def foo2(my_liste):
my_liste[:] = []
> foo(liste)
>
> print liste# she expected liste to be an empty list
>
> ### Perl ###
>
> @lst =(1,2,3);
> $liste =\@lst;
> foo($liste);
> print "@lst\n";
>
> sub foo {
> my($my_list)=@_;
> @{$my_list}=()
> }
>
> I have to admit that I don't know how to clearly explain to her the
> differences between these results.
> Could someone please help us understand these difference between
> Python and Perl ?
>
> Thanks in advance,
> P4|1ndr0m
--
David C. Ullrich
More information about the Python-list
mailing list