[Israel.pm] A bit about Python

Yosef Meller mellerf at netvision.net.il
Mon Nov 16 22:20:37 PST 2009

Shmuel Fomberg wrote:
> Hi All.


> I have been looking on Python in the last weeks, and a few thoughts:

I have been working with python for two years now, and here's mine:

> they (the python world) hate mini-languages, and prefer objects with a 
> lot of methods.
> for example, instead of sprintf("%10s", ...) they have string.ljust method.

however, you can do:
s = "Hello %s, I'm %d years old" % ("world!", 28)
where the % operator for strings does format-string substitution.

> The closure mechanizem is pretty weak, and can be better describe as 
> constants placing, and is reduced to one liner.
> (something like: lambda x: x * value)

This is what I remember about closures from perl, and I use closures in 
Python for eveerything I did in Perl. What can you do with a perl 
closure more than this?

Oh, the one thing you can't do in Python is declare a complicated 
*anonymous* (lambda) sub, but then you have lexical subs:

def generate_closure(x):
   def complicated():
     a = x**2
     return math.sqrt(a)
   return complicated

> Trying to cover for the weak closure, they have a 'generator' functions, 
> with special command - yield.
> def Counter(start)
>    x = start
>    while x < 7:
>        yield x
>        x = x + 1
> so after we create the Counter, every time we call Counter.next the 
> program return to the loop and run until the yield command.
> As much as I can see there is no way to pass parameter to the next command.

Is there a Perl equivalent?

> in Python, there is a map and filter functions. they don't like them 
> very much, and the new syntex is:
> newList = [e*2 for e in oldList if e > 3]
> I don't know if I like this writing, because the flow zig-zags. starts 
> with the "for e in oldList", then goes to the right with "if e > 3", and 
> then to the left with "e*2".
> after years of maping and greping I'm used to see data flows in one 
> direction.

The Perl syntax is also better in my opinion because it is less 
cumbersome for chaining multiple maps/greps, while the Python way starts 
to get messy after one pair. A bit surprising in a language that values 
code clarity so much :)

> A weird thing about objects and inheritance: when calling a super's 
> function, we can't call $self->SUPER::func(...) of like Moose super(). 
> (if I remember correctly) we call the super function by name. 
> MyBase.func(self, ...).

You have super() in Python, and it has one of those "considered harmful" 

> And I'm missing CPAN. they don't have a main archive of modules, but 
> every module have his own website.

To my understanding, PyPI (the Python Package Index) is an effort to 
create a CPyAN, but it just doesn't have the entrenched cultural place 
of CPAN in Perldom

> Good morning.

It was :)
Good day,

More information about the Perl mailing list