[Israel.pm] A bit about Python

Shmuel Fomberg semuelf at 012.net.il
Wed Nov 18 12:31:52 PST 2009


Hi Yosef.

>> 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.

I'm aware of this operator, and I'm also sure that it support the full C 
printf spec. (probably minus the weird cases)
The point here is not of power - more of style. where in other languages 
we work with a cheat sheet for the printf format, in Python there are 
bunch of methods to do the same thing - just more readable.

>> The closure mechanizem is pretty weak, and can be better describe as 
>> constants placing, and is reduced to one liner.
> 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?

I tried to define in Python:
def JumpingCounter(start):
    x = start;
    def func(jump):
       x = x + jump
       return x
    return func

It failed to compile. the reason, as much as I see it, is because 'x' 
become a constant for func after JumpingCounter ended.
In Perl, it would have worked. the captured variable stays a variable.

I have been thinking about it a little, and as much as I can remember, 
every time I used closure in Perl, it was either a generator or a 
constant-closure. And most people have problems "getting" closures. so 
maybe it is not a bad idea to break this power tool to two weaker tools 
that are more focused.

>> 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" 
> articles:
> http://fuhm.net/super-harmful/

hmmm. In Perl we have the SUPER metaclass, and IIRC it was "fixed" by 
the SUPER CPAN module, and Moose created their own super command.
Problematic staff, it seen to be...

Another interesting subject is Tuples.
Tuples are arrays, more like constant arrays.
And I was very suprised to read that I can use them as hash keys. at 
first it sound weird, but then I remembered all the times that I have 
created string out of multiple values and used it as hash key.

Good night,
Shmuel.


More information about the Perl mailing list