Tuesday, October 03, 2006

How do python people get by without sigils?

Just a thought but a lack of sigils makes python code look a bit funny. :)

I also wrote some C code today and that looked a bit odd too. :)

7 comments:

Lakin Wecker said...

The same way that perl people get by without using variable names.


...


...



...


...


Yes. I'm talking about $_

Lakin Wecker said...

So that means you could write a complete perl program without sigils. Right?


while(<STDIN>) {
chomp;
print;
}

Lakin Wecker said...

Btw, have you ever considered changing your username to "Ranti*TROLL*inator"? ;)

Rantinator said...

1)
The sigil thing is like when people the prefix system like "iTmp" where the "i" means integer and "Tmp" is the variable name.

At first it seems silly to put "i" there. Your code should not have wide enough scopes that you forget.

After using sigils for a while the mind uses them for compression. I don't have to remember that "%records" is a hash instead of an array.

They are also useful because you can use them as shorthand to change what you want the interpretor to do with a character or two.

2)
It's taking some time to get into reading python code. It's like I mentally have flow-arrows overlaid on the code. (I'm not kidding.)

I think this is a mental pattern system. It's like how when many phone numbers in an area have the same first three digits, say 314 and 271. I learn something like "prefix #1 and 1234" for the number 314-1234.

The structure of code in different languages has different patterns. I can't see the patterns yet.

3)
That is a good point about the implicit variables. I rarely do it. Some perl programmers recommend writing code that doesn't need variable names.

4)
I was going to comment on how confusing the syntax for creating a closure is in Python (version 2) but maybe that will be going too far. ;)

After all the Perl code probably doesn't make much sense to someone without a bunch of experience in Perl.

sub outer($) {
my $x = shift;
my $c = sub { ++ $x; return $x; };
return $c;
}

my $first = outer(0);
print "a: " , &$first , "\n";
print "b: " , &$first , "\n";
print "c: " , &$first , "\n";

Actually related to closures the local/global/builtin scoping seems odd but I haven't tried using it much. (Perl and CommonLisp support both lexical and dynamic scoping.)

Lakin Wecker said...
This comment has been removed by a blog administrator.
Lakin Wecker said...

1) You're right: reduce your scope. In python it's often said: "If it acts like a duck, you can assume it's a duck". As directly opposed to: "If it looks like a duck, you can ssume it's a duck".

2) Good point. Switching paradigms is definitely hard work. I find that perl has so many differing paradigms that it's hard to know which is in effect and where.

For my purposes, python has succeeded in reducing the number of paradigms to a manageable level. Guido et. al do an amazing job of keeping the paradigm level low.

4) In my mind, closures simply mean: Grouping state with algorithms. and immediately I think ... Why not use an object, a class? that's what they're for! In my mind closures are another paradigm to solve the same thing that objects already do an amazing job for. For that reason alone, I feel that closures are useless.

Here some equivalent python code that is clearer (for anyone who knows OO, not just for people who know "perl-style-closures"). If I group statements together like you have, it's actually two lines shorter.

class outer:
. def __init__(self,x):
. . self.x = x
. def add_one(self):
. . self.x +=1
. . return self.x

first = outer(0).add_one

print "a: ", first()
print "b: ", first()
print "c: ", first()

Mike said...

It sould be called "nextNumber" not "first"... actually, i think it should look like this:

class Counter:
. def __init__(self,x): self.x = x
. def nextNumber(self): self.x +=1; return self.x

counter = Counter(0)

print "a: ", counter.nextNumber()
print "b: ", counter.nextNumber()
print "c: ", counter.nextNumber()