Typed Python?

  • Thread starter Thomas Reichelt
  • Start date
B

beliavsky

Thomas Reichelt said:
Moin,

short question: is there any language combining the syntax, flexibility and
great programming experience of Python with static typing? Is there a
project to add static typing to Python?

Thank you,

If you are using Python mainly for numerical work and use Numeric or
Numarray for array operations, Fortran 90 or 95 may be the closest
replacement with static typing. The syntax is fairly similar (assuming
that the free source form of F90/F95 is used, as it should for new
code). Fortran 2003 supports object-oriented programming with
inheritance (as does Python), but full F2003 compilers are not
available yet.
 
F

Fernando Perez

Paul said:
To put it another way: Jarek complains that static typing would destroy
the "fun". I think he means Python's flexibility. A type inferencer
removes the need to declare types but a statically type-inferenced
language is still statically typed. It will still be strict about type
usage.

I am one who would really like _optional_ static typing information to be
available in python, but for different reasons than the usually requested here:
performance. My field is scientific computing, and when you are inside a
simple for loop, manipulating arrays of homogeneous data, the overhead of
python's dynamicism is a killer. Obviously what ends up happening is that we
rewrite those parts of our codes in C/C++/Fortran, but it would be great to be
able to simply tell python the types of the variables and have it not do any of
its dynamic runtime checks.

I do not believe in static typing as a compile-time 'feature': I've grown to
feel that all the supposed benefits of static typing can indeed be achieved
with proper testing, and the _huge_ gains in flexibility and productivity one
gets from python's type model are something I don't want to do without.

But for tightly constrained code that needs to run as fast as possible, without
repeating any unnecessary work (such as type detection), having an option to
declare type information would be a huge boon.

Cheers,

f
 
N

Nicolas Fleury

Fernando said:
I am one who would really like _optional_ static typing information to be
available in python, but for different reasons than the usually requested here:
performance. My field is scientific computing, and when you are inside a
simple for loop, manipulating arrays of homogeneous data, the overhead of
python's dynamicism is a killer. Obviously what ends up happening is that we

For performance, maybe Psyco is what you need, without the need fo
static typing, but rather different compilation of code for different
types, like with C++ templates.

Regards,
Nicolas
 
F

Fernando Perez

Nicolas said:
For performance, maybe Psyco is what you need, without the need fo
static typing, but rather different compilation of code for different
types, like with C++ templates.

Psyco sounds appealing, but it's an x86-only solution. That won't be of much
help on a Cray or an Itanum cluster...

Cheers,

f
 
F

Fernando Perez

Fernando said:
Psyco sounds appealing, but it's an x86-only solution. That won't be of much
help on a Cray or an Itanum cluster...
Itanium

stupid typo.

f
 
A

Arthur

Arthur> "Python is easy, and learning to program with Python is
Arthur> easy."

Arthur> Often implying that it is only that others are making the
Arthur> effort in languages other than Python that makes it seem
Arthur> otherwise.

Arthur> Which is wonderful to hear, and would be worth saying, if
Arthur> saying it would make it true.

Arthur> Of course, it doesn't.

Arthur> Of course, its nonsense.

Well, many of us just think it's true. Only the academics seem to
think Scheme is easier to learn than, say Python or even C. Students
often disagree.

I don't think the academics you are referring to think Scheme is
easier to learn than Python, I'm thinking that they might be thinking
that neither is *easy*, and easy isn't the point in any case.

It is precisely this promotion as "easy" as somehow representing an
ultimnate good, in the context of education, that I find frustrating
to hear repeated so often here.

Is that the most important thing that Python has to offer?

It seems to me that in many respects Python is a programmer's
programming language. Isn't it safe to say that most people who use
Python, learned their fundamentals elsewhere, and find Python as a
productive way of applying what they already know.

This would be consistent with something I believe I read Guido as
having said in a recent interview. That Python was originally
developed as a tool for C programmers. ABC roots notwithstanding

I think I can be articulate as to why Python can be a good tool to
apply to educational challenges. But "easy" is never a word that
comes quickly to mind.

"Glue" comes to mind.

"Fun" comes to mind - and I see nothing wrong with *that* word in an
educational context.

It happens that *I* have learned Python and not learned Scheme, or C.

Maybe precisely *because* it is messier than either. ;)

Art
 
?

=?iso-8859-1?Q?Fran=E7ois?= Pinard

[Ville Vainio]
[Chain Lube]
The code is much better structured in Scheme than in Python. It is a
big mistake to assume that Python displays any readability.
With all due respect, someone that thinks [...]

Flamatory comments are not really my bag. :)

But as someone who much liked both Scheme and Python, let me stress a
few aspects where Python attracted me more than Scheme.

The most important one is the bulk of library code which comes standard
with Python, but which has to be collected or rewritten in Scheme, not
being part of the standard.

Another is that, despite Scheme attempts to be minimalist (something
that I much like), Python is -- or at least once was! :) -- more on the
side of having only one way of doing things. Two examples come to mind.

Scheme has three ways to write a conditional statement: `(cond ...)',
`(if ...)' and `(case ...)'. All have virtues and drawbacks, and I often
switch between the three, depending on my code, and the edits I do on my
code, so it stays best at all time. This is annoying.

Scheme has many ways for looping while changing local variables, some
of which are less nice and which I did not use much, I'm not even
discussing them. The `(let loop ...)' construct is useful for writing
tail-recursive loops without creating a flurry of little separate
functions, but it is often clearer writing destructive `(set! ...)'
instead of always being tail-recursive, even if tail-recursivity
often yields more efficient compilation. When you mix assignment and
tail-recursion, the amount of solutions for a piece a code is near
exponential in the number of variables. I found this annoying, too.

Bindings, conditionals and loops are very basic and ubiquitous in any
programming language, and Python got these relatively simpler than
Scheme. Scheme may be minimalist overall, but because of the frequency
of the above constructs in a program, the minimalism is not so apparent.

P.S. - On the other side, in those rare circumstances one needs
continuations, Scheme offers them very easily, while there are rather
painful to simulate in standard Python. Also, there once was a notable
distance between Scheme and Python about lazy evaluation and closure
construction, but more recent versions of Python offer fairly tractable,
almost easy approaches for such things.
 
?

=?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=

Try numeric / numarray (optimized arrays/matrices of real/complex for
scientific computing).
 
V

Ville Vainio

Arthur> I don't think the academics you are referring to think
Arthur> Scheme is easier to learn than Python, I'm thinking that
Arthur> they might be thinking that neither is *easy*, and easy
Arthur> isn't the point in any case.

Actually, the "real" attitude of the lecturers is often "now everyone
knows C (or C++, ot Java, or...) already, let's look at something
completely different". And apparently, the number of schools using
Scheme is constantly dropping - it has either been dropped already, or
the schools are considering dropping it. Usually replacing it for
Java, but they would do better to replace it with Python, of course.

Arthur> It is precisely this promotion as "easy" as somehow
Arthur> representing an ultimnate good, in the context of
Arthur> education, that I find frustrating to hear repeated so
Arthur> often here.

Perhaps. Perhaps you read it as "easy as in VB", while it actually
means "easy to learn, easy to do everything".

Arthur> It seems to me that in many respects Python is a
Arthur> programmer's programming language. Isn't it safe to say
Arthur> that most people who use Python, learned their
Arthur> fundamentals elsewhere, and find Python as a productive
Arthur> way of applying what they already know.

Quite probably, but that's more because of the current status of the
language as a "grassroots" language, where savvy programmers check it
out and start using it as opposed to many languages that are being
pushed by authorities (schools, conservative companies etc.).

Arthur> It happens that *I* have learned Python and not learned
Arthur> Scheme, or C.

Arthur> Maybe precisely *because* it is messier than either. ;)

Frankly, there is no real reason to learn Scheme. Common Lisp, OTOH,
has at least a theoretical possibility of being useful in the real
world (apparently Debian even provides some *gasp* libraries for
Common Lisp).

C itself is probably not too messy (awkward and clumsy, yes). Programs
written in C are.
 
J

Jacek Generowicz

Fernando Perez said:
I am one who would really like _optional_ static typing information
to be available in python, but for different reasons than the
usually requested here: performance. My field is scientific
computing, and when you are inside a simple for loop, manipulating
arrays of homogeneous data, the overhead of python's dynamicism is a
killer. Obviously what ends up happening is that we rewrite those
parts of our codes in C/C++/Fortran, but it would be great to be
able to simply tell python the types of the variables and have it
not do any of its dynamic runtime checks.

I do not believe in static typing as a compile-time 'feature': I've
grown to feel that all the supposed benefits of static typing can
indeed be achieved with proper testing, and the _huge_ gains in
flexibility and productivity one gets from python's type model are
something I don't want to do without.

But for tightly constrained code that needs to run as fast as
possible, without repeating any unnecessary work (such as type
detection), having an option to declare type information would be a
huge boon.

I guess that you know that this sort of thing is successfully done in
Common Lisp (and Dylan and probably some, if not all, Schemes).

I've even heard rumours of Guido saying that something like it will
appear in a future, Python.

I would certainly warmly welcome it, if it should arrive in Python.
 
J

Jacek Generowicz

Ville Vainio said:
Only the academics seem to think Scheme is easier to learn than, say
Python or even C. Students often disagree.

Sounds great; I bet it makes a lot of Python fans feel warm and fuzzy
inside. Unfortunately the claim is completely unfounded.

You should take a look at what the TeachScheme people have been doing,
and what they seem to be achieving.
 
J

Jacek Generowicz

Ville Vainio said:
Chain> The code is much better structured in Scheme than in
Chain> Python. It is a big mistake to assume that Python displays
Chain> any readability. The one

So true.

(set! x 10)

Is so much more readable than

x=10

Well, that rather depends on what you are used to. But it is
undeniably true that the former is better structured.
 
P

Peter Maas

Jacek said:
Well, that rather depends on what you are used to. But it is
undeniably true that the former is better structured.

You mean the expression

"(set! x 10)" is_better_structured_as "x=10"

is true? How is is_better_structured_as defined? :)

Mit freundlichen Gruessen,

Peter Maas
 
P

Peter Hansen

Peter said:
You mean the expression

"(set! x 10)" is_better_structured_as "x=10"

is true? How is is_better_structured_as defined? :)

I think you mean "is_better_structured_than", not "_as".

What you wrote, if true, would mean that Jacek meant
"x=10" was the better structured one...

(Anyway, arguing over "structured" is silly. Clearly "more readable"
rules. ;-)

-Peter
 
R

Reinhold Birkenfeld

Peter said:
You mean the expression

"(set! x 10)" is_better_structured_as "x=10"

is true? How is is_better_structured_as defined? :)

Hey, it is

(is_better_structured_as "(set! x 10)" "x=10")

Be consistent!

Reinhold
 
A

Arthur

Well, that rather depends on what you are used to. But it is
undeniably true that the former is better structured.

Well it does seem to me that the subtleties of assignment in Python is
an excellant example of where its "easiness" is more apparent than
real.

The notice of which takes nothing away from Python.

But does work toward dispute of certain discriptions of it.

Art
 
V

Ville Vainio

Jacek> Sounds great; I bet it makes a lot of Python fans feel warm
Jacek> and fuzzy inside. Unfortunately the claim is completely
Jacek> unfounded.

Jacek> You should take a look at what the TeachScheme people have
Jacek> been doing, and what they seem to be achieving.

I took a look at the TeachScheme website. One example program in
particular caught my eye in http://www.teach-scheme.org/Talks/.

the Scheme version:

( define ( guest name list )
( cond
( ( empty? list ) ?no )
( ( equal? name ( first list )) ?yes )
( else ( guest name ( rest list )) ) ))


C/C++:

------

#include <stdio.h>
typedef struct listCell * list;
struct listCell {
int first;
list rest;
};
bool guest (int x, list l) {
if (l == NULL)
return false;
else if (x == (l -> first))
return true;
else
return guest (x, l -> rest);
}
int main (int argc, char ** argv) {
list l1, l2, l3 = NULL; int x;
l1 = (list) malloc (sizeof (struct listCell));
l2 = (list) malloc (sizeof (struct listCell));
l2 -> first = 3; l2 -> rest = l3;
l1 -> first = 2; l1 -> rest = l2;
scanf ("%d", &x);
printf ("%d\n", member (x, l1));
}
----------

So they implement a linked list data type in C/C++ version, and use
the native/natural linked list (i.e. just a list) in Scheme. That's
kinda weasely road to take, don't you think? Especially if they use
this as marketing material for non-techies...

And here's the equally weaselish Python version, which they
unsurprisingly don't include:
 
O

Oliver Fromme

Donn Cave said:
> I haven't tried it, but Nice is a Java based language with a more
> sophisticated type system than Java, and a few extra conveniences.
>
> http://nice.sourceforge.net/
>
> It doesn't appear to have type inference, which so far as I know
> may be exclusive to functional languages, [...]

Just for the record, there's a dialect of C called Cyclone,
which supports type-inference (although with some limitations).
Since it's derived from C, it's not a functional language, but
it has grown quite some features typically found in functional
languages.

http://www.eecs.harvard.edu/~greg/cyclone/index.html

There's a mirror at AT&T:

http://www.research.att.com/projects/cyclone/

Best regards
Oliver
 
E

Erik Max Francis

Oliver said:
Just for the record, there's a dialect of C called Cyclone,
which supports type-inference (although with some limitations).
Since it's derived from C, it's not a functional language, but
it has grown quite some features typically found in functional
languages.

http://www.eecs.harvard.edu/~greg/cyclone/index.html

Its extra pointer qualification features are interesting. Thanks for
bringing this to my attention, I don't think I'd heard of it before.
 
J

Jeremy Bowers

You mean the expression

"(set! x 10)" is_better_structured_as "x=10"

is true? How is is_better_structured_as defined? :)

def is_better_structured_than(a, b): # take Peter's suggestion
"Returns true if a is better structured than b."
if not equivalent(a, b):
raise ThatsNotFairError

return len(a) > len(b)


(It is the only logical interpretation of the statement since in any sane
parse tree of the two on some fair ground they are indistinguistable. If
you don't like this logic, Jacek, I recommend meditating on your knowledge
of computer science, not blaming me; I mean that first sentence quite
literally.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top