[Python-il] Perl Vs. Python on Various Points

Roman Gaufman hackeron at gmail.com
Mon Jul 13 16:19:12 IDT 2009


>
> 1. Syntax as an Indicative of What the Language is Doing:
> ---------------------------------------------------------
>
> He said he didn't like Perl syntax like "push @$array_ref, $val;" because
> of the sigils. I said I happen to like it because the extra characters
> convey meaning. By looking at this code I know that $array_ref is an array
> reference, that it is being treated as an array and that one appends a
> single ("scalar") value to it. On the other if I see code like this:
>
> <<<<<
> s.add(h)
>>>>>>

it's .append and if you see it you can assume it's an array or look at
the context. For example:

list = ['a', 'b', 'c']
hash = {}
hash['List'] = list
print hash['List']

vs

my @list = ('a', 'b', 'c');
my %hash;
$hash{'List'} = \@list;
print "@{$hash{'List'}}\n";

I find both reading and writing the python version easier - it's all
personal taste though.


>
> 2. Comparison Operators:
> ------------------------
>
> Later on the discussion diverted to comparison operators. Now python only
> has "==" and friends for comparison (at least as far as I know) while Perl 5
> has both ==/!=/>/etc. and eq/ne/gt/etc. The first ones are intended for
> numeric comparison and the latter ones for string comparison.
>
> I argued that by looking at code with such comparisons, I can tell what kind
> of comparison the programmmer intended the comparison to be. Part of the
> reason for the fact that Perl 5 has both types of comparison is that it
> does not have separate data types for strings and for numbers, but this is
> not the only reason.
>
> So in Python, I have:
>
> <<<<<<<<<<<<<<<
> shlomi:~$ python
> Python 2.6.2 (r262:71600, Jul 11 2009, 07:37:11)
> [GCC 4.4.0] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> 1 == 1.0
> True
>>>> "1" == "1.0"
> False
>>>>
>>>>>>>>>>>>>>>>
>
> Whereas in Perl, I have:
>
> <<<<<<<<<<<<<<<
> shlomi:~$ re.pl
> $ 1 == 1.0
> 1
> $ "1" == "1.0"
> 1
> $ "1" eq "1.0"
>
> $ 1 eq 1.0
> 1
>>>>>>>>>>>>>>>>
>
> That's not all there is to it, however. In Python:
>
> <<<<<<<<<<<<<<<<<<<<<
> shlomi:~$ python
> Python 2.6.2 (r262:71600, Jul 11 2009, 07:37:11)
> [GCC 4.4.0] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> x = [0,1,2]
>>>> y = [2,1,0]
>>>> y.reverse
> <built-in method reverse of list object at 0xb7cc444c>
>>>> y.reverse()
>>>> x
> [0, 1, 2]
>>>> y
> [0, 1, 2]
>>>> x == y
> True
>>>>
>>>>>>>>>>>>>>>>>>>>>
>
> So Python's == does a deep comparison of complex data structures and returned
> that x and y where equivalent despite the fact that they aren't the same
> physical reference.
>
> In Perl, however:
>
> <<<<<<<<<<
> shlomi:~$ re.pl
> $ [0,1,2] eq [0,1,2]
>
> $ ([0,1,2] eq [0,1,2]) ? "True" : "False"
> False
> $ ([0,1,2] == [0,1,2]) ? "True" : "False"
> False
> $
>>>>>>>>>>>
>
> (You shouldn't use == for comparing references in Perl 5 - it's just for the
> sake of the demonstration.)
>
> Perl did a shallow comparison of the references and returned a false because
> they weren't the same reference.
>
> I should note that in Perl comparison is not necessarily O(1) because if I
> have
> two very long strings, then comparing them may be O(N) where N is the length
> of the strings.
>
> For deep comparison we have CPAN modules like
> http://search.cpan.org/dist/Test-Differences/ , or can use the more limited
> is_deeply() functionationality of Test::More.
>
> I personally feel that it's impossible to have "one-comparison-fits-all"
> because for two pieces of data, there may be several ways that we would like
> to compare them.

If you know to know if it's the same reference, use "is":

[1,2,3] is [1,2,3]
False



More information about the Python-il mailing list