473,388 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to best explain a "subtle" difference between Python and Perl ?

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 ):
my_list = []

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
Aug 12 '08 #1
8 2404
In article
<42**********************************@s50g2000hsb. googlegroups.com>,
Palindrom <de*******@gmail.comwrote:
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
Aug 12 '08 #2
Palindrom wrote:
### Python ###

liste = [1,2,3]

def foo( my_list ):
my_list = []
The above points the my_list reference at a different object. In this
case a newly created list. It does not modify the liste object, it
points my_list to a completely different object.
### Perl ###

@lst =(1,2,3);
$liste =\@lst;
foo($liste);
print "@lst\n";

sub foo {
my($my_list)=@_;
@{$my_list}=()
}
The above code *de-references* $my_list and assigns an empty list to its
referant (@lst).

The two code examples are not equivalent.

An equivalent perl example would be as follows:

### Perl ###

@lst =(1,2,3);
$liste =\@lst;
foo($liste);
print "@lst\n";

sub foo {
my($my_list)=@_;
$my_list = [];
}

The above code does just what the python code does. It assigns a newly
created list object to the $my_list reference. Any changes to this now
have no effect on @lst because $my_list no longer points there.

n
Aug 12 '08 #3
Thank you Nigel, it's clearer for both of us now.
I think wat confused her is the fact that :

L = [1,2,3]
def foo(my_list):
my_list.append(4)

will modify L, while the following:

L = [1,2,3]
def foo(my_list):
my_list = [1,2,3,4]

will not.

On Tue, Aug 12, 2008 at 6:46 PM, Nigel Rantor <wi****@wiggly.orgwrote:
Palindrom wrote:
>>
### Python ###

liste = [1,2,3]

def foo( my_list ):
my_list = []

The above points the my_list reference at a different object. In this case a
newly created list. It does not modify the liste object, it points my_list
to a completely different object.
>### Perl ###

@lst =(1,2,3);
$liste =\@lst;
foo($liste);
print "@lst\n";

sub foo {
my($my_list)=@_;
@{$my_list}=()
}

The above code *de-references* $my_list and assigns an empty list to its
referant (@lst).

The two code examples are not equivalent.

An equivalent perl example would be as follows:

### Perl ###

@lst =(1,2,3);
$liste =\@lst;
foo($liste);
print "@lst\n";

sub foo {
my($my_list)=@_;
$my_list = [];
}

The above code does just what the python code does. It assigns a newly
created list object to the $my_list reference. Any changes to this now have
no effect on @lst because $my_list no longer points there.

n
Aug 12 '08 #4
Thank you both for your reply !

On Tue, Aug 12, 2008 at 8:52 PM, Demarche Bruno <de*******@gmail.comwrote:
Thank you Nigel, it's clearer for both of us now.
I think wat confused her is the fact that :

L = [1,2,3]
def foo(my_list):
my_list.append(4)

will modify L, while the following:

L = [1,2,3]
def foo(my_list):
my_list = [1,2,3,4]

will not.

On Tue, Aug 12, 2008 at 6:46 PM, Nigel Rantor <wi****@wiggly.orgwrote:
>Palindrom wrote:
>>>
### Python ###

liste = [1,2,3]

def foo( my_list ):
my_list = []

The above points the my_list reference at a different object. In this case a
newly created list. It does not modify the liste object, it points my_list
to a completely different object.
>>### Perl ###

@lst =(1,2,3);
$liste =\@lst;
foo($liste);
print "@lst\n";

sub foo {
my($my_list)=@_;
@{$my_list}=()
}

The above code *de-references* $my_list and assigns an empty list to its
referant (@lst).

The two code examples are not equivalent.

An equivalent perl example would be as follows:

### Perl ###

@lst =(1,2,3);
$liste =\@lst;
foo($liste);
print "@lst\n";

sub foo {
my($my_list)=@_;
$my_list = [];
}

The above code does just what the python code does. It assigns a newly
created list object to the $my_list reference. Any changes to this now have
no effect on @lst because $my_list no longer points there.

n
Aug 12 '08 #5
On Aug 12, 9:17*am, Palindrom <demarc...@gmail.comwrote:
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 ):
* * my_list = []

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 ?
David Ullrich gives a great and complete answer. I want to focus on
some of the subtleties.

Perl and C share a lot in common. There are "direct" variables, things
like numbers and arrays. These aren't references to object, but the
object is itself stored in the variable. That is, you can't talk about
the thing that is '@lst' without creating a reference to it.

Python, on the other hand, doesn't have direct variables. Well, it
does, it is just that all of the direct variables are really pointers
or references to the values they reference.

Imagine a perl where you are only allowed to use scalars, and
specifically, scalars that are references to object but not references
to references. That is the Python variable system.

To be more concrete...

This statement cannot be expressed in Python:

@lst = (1, 2, 3); # perl
However, you can create an arrayref (perl) / list (Python) and assign
a scalar (perl) / variable (Python) to reference it:

$liste = [1, 2, 3]; # perl
liste = [1, 2, 3] # Python
Likewise, this statement cannot be expressed in Python:

$refref = \$ref; # perl
Although you can cheat in both perl and Python to get a similar
result:

$refref = [$ref] # perl
refref = [ref] # python
As far as the functions, the Python version and the perl version are
doing two completely different things. David explains how to write a
Python version that does what the perl version is doing. If you wanted
a perl version that did what your python version did, it would look
like this:

sub foo {
my ($my_list) = @_;
$my_list = [];
return undef;
}
Is Python's variable system better than perl's? It depends on which
way you prefer. As for me, being a long-time veteran of perl and
Python, I don't think having a complicated variable system such as
perl's adds anything to the language. Python's simplicity in this
regard is not only sufficient, but preferable.
Aug 13 '08 #6
On Aug 13, 2:12*am, Jonathan Gardner <jgard...@jonathangardner.net>
wrote:
On Aug 12, 9:17*am, Palindrom <demarc...@gmail.comwrote:
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 ):
* * my_list = []
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 clearlyexplainto her the
differences between these results.
Could someone please help us understand these difference between
Python and Perl ?

David Ullrich gives a great and complete answer. I want to focus on
some of the subtleties.

Perl and C share a lot in common. There are "direct" variables, things
like numbers and arrays. These aren't references to object, but the
object is itself stored in the variable. That is, you can't talk about
the thing that is '@lst' without creating a reference to it.

Python, on the other hand, doesn't have direct variables. Well, it
does, it is just that all of the direct variables are really pointers
or references to the values they reference.

Imagine a perl where you are only allowed to use scalars, and
specifically, scalars that are references to object but not references
to references. That is the Python variable system.

To be more concrete...

This statement cannot be expressed in Python:

*@lst = (1, 2, 3); * *# perl

However, you can create an arrayref (perl) / list (Python) and assign
a scalar (perl) / variable (Python) to reference it:

*$liste = [1, 2, 3]; *# perl
*liste = [1, 2, 3] * *# Python

Likewise, this statement cannot be expressed in Python:

*$refref = \$ref; * * # perl

Although you can cheat in both perl and Python to get a similar
result:

*$refref = [$ref] * * # perl
*refref = [ref] * * * # python

As far as the functions, the Python version and the perl version are
doing two completely different things. David explains how to write a
Python version that does what the perl version is doing. If you wanted
a perl version that did what your python version did, it would look
like this:

*sub foo {
* *my ($my_list) = *@_;
* *$my_list = [];
* *return undef;
*}

Is Python's variable system better than perl's? It depends on which
way you prefer. As for me, being a long-time veteran of perl and
Python, I don't think having a complicated variable system such as
perl's adds anything to the language. Python's simplicity in this
regard is not only sufficient, but preferable.
Thank you Jonathan for your extensive and clear response !

I agree with you, Python's variable system is eazier to understand.
Regards.
Aug 13 '08 #7
Jonathan Gardner wrote:
[...eloquent and interesting discussion of variable system snipped...]
>
Is Python's variable system better than perl's? It depends on which
way you prefer. As for me, being a long-time veteran of perl and
Python, I don't think having a complicated variable system such as
perl's adds anything to the language. Python's simplicity in this
regard is not only sufficient, but preferable.
Very well put.

I am not however sure I agree with your very final thought.

I ma a long time C/C++/Java/Perl developer. I know some python too.

The Python system is the same as the Java system, apart from Java's
primitive types, which is a completely different discussion that I
really don't want to get into right now.

So, everything is by reference.

I understand, and agree that a simple system is good. And maybe even
preferable. But it isn't always sufficient.

Some algorithms are much easier to write if you know that your
parameters are going to be copied and that the function may use them as
local variables without having to explicitly create copies.

You can also reason more easily about what side-effects the function
could have if you know it cannot possibly modify your parameters.

Other systems out there require pointer-like semantics (for example
CORBA out and inout parameters) which have to be kludged in languages
like Java to pass in wrapper objects/boxes that can be assigned values.

Whilst it may be easier to learn a system like python/java, in the end
the amount of time spent learning the system is normally dwarfed by the
time spent using the system to build software. I would rather have a
type system that is as expressive as possible.

Also, note that it is entirely possible to create many, many, many
interesting and useful things in Perl without having to resort to
references. They are a relatively new feature after all.

Just my 0.02p

n
Aug 13 '08 #8
Nigel Rantor wrote:
The Python system is the same as the Java system, apart from Java's
primitive types, which is a completely different discussion that I
really don't want to get into right now.

So, everything is by reference.
I am not too familiar with Java's system.
I understand, and agree that a simple system is good. And maybe even
preferable. But it isn't always sufficient.

Some algorithms are much easier to write if you know that your
parameters are going to be copied and that the function may use them as
local variables without having to explicitly create copies.
If you could provide some specific examples, I would appreciate that.

As for me, using Python (and perl, which doesn't make copies when you
are passing references around) extensively for large projects, I've
never run into a case where that's bitten me. If I need to make a copy
of any object, it's a one-line statement in Python. It's pretty rare
that you do need to make copies of stuff, and it's pretty obvious where
it needs to be done. I'm glad that it's very explicit.
You can also reason more easily about what side-effects the function
could have if you know it cannot possibly modify your parameters.
We can all keep track of where our hands are by wearing straitjackets as
well.

As for me, I'd rather make vague promises and use my best judgment with
complete freedom to deliver the best result.
Other systems out there require pointer-like semantics (for example
CORBA out and inout parameters) which have to be kludged in languages
like Java to pass in wrapper objects/boxes that can be assigned values.

Whilst it may be easier to learn a system like python/java, in the end
the amount of time spent learning the system is normally dwarfed by the
time spent using the system to build software.
I disagree with this. I find myself many factors more productive in
Python than expert Java developers. I also find that I think about the
problem while the Java developers are always thinking about how they can
express themselves in Java properly.

You may want to spend some more time in Python. Once you abandon all of
your Java-isms and perl-isms, then you'll find it quite relaxing. Most
of my ramp up time in Python was leaving behind old ideas that were
specific to the languages I already knew. It takes a while for one to
realize that programming isn't supposed to be hard.
I would rather have a type system that is as expressive as possible.
Then you really should look at Haskell.
Also, note that it is entirely possible to create many, many, many
interesting and useful things in Perl without having to resort to
references. They are a relatively new feature after all
It's true that Perl's reference system is relatively new, but I can't
imagine writing even trivial scripts without references. (How do you do
getopt without references?) The hacks people had to do in the past were
horrifying. Yes, it's possible. No, it's not preferable.
Aug 13 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: RHNewBie | last post by:
Hello, What is the difference between null and NULL. Are x == null and x == NULL the same? The compiler is gcc 3.2. Thanks.
5
by: Bob | last post by:
Are they different names for the same concept ?
17
by: John Grandy | last post by:
What is the C# equivalent to the VB.Net looping structure: Protected rbl As RadioButtonList Dim li As ListItem For Each li In rbl.Items Next li
7
by: Dave Benjamin | last post by:
There's been a lot of discussion lately regarding Ruby and the notion of a "humane" interface to objects like arrays and maps, as opposed to "minimalist" ones. I believe the article that started...
5
by: chandanlinster | last post by:
consider the program, /*********************************************************/ int main() { int a; printf("a = %u\n", a); printf("*a = %u\n", *a); return 0; }
19
by: Neil Cerutti | last post by:
Where can I find documentation of what Python accepts as the filename argument to the builtin function file? As an example, I'm aware (through osmosis?) that I can use '/' as a directory...
2
by: swingingming | last post by:
Hi, When I am playing around with options, I came across with enabling "SQL server compatible syntax". After I enable this, I could not get one of my query to run. Can anyone explain the subtle...
7
by: SteveB | last post by:
Hi All, I have a number of databases which I would like to convert to VB or VC using Microsoft Visual Studio Express. I know I cannot just click a button and have everything done automatically...
4
by: Peter | last post by:
Hi I've been delving into "delegates" and "anonymous methods", and now I've come across the term "closure". Some information I've found says that C# does not have closures, other information...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.