B
bearophileHUGS
I often use Python to write small programs, in the range of 50-500
lines of code. For example to process some bioinformatics data,
perform some data munging, to apply a randomized optimization
algorithm to solve a certain messy problem, and many different things.
For that I often use several general modules that I have written, like
implementation of certain data structures, and small general "utility"
functions/classes, plus of course several external modules that I keep
updated.
Compared to other languages Python generally allows me to write a
correctly working program in the shorter time (probably because of the
Python shell, the built-in safeties, the doctests, the clean and short
and handy syntax, the quick write-run-test cycle, the logical design,
its uniformity, the availability of standard data structures and many
standard or external modules, and the low number (compared to other
languages) of corner cases and tricky semantic corners).
Today Python is defined a dynamic language (and not a scripting
language, a term that few languages today seem to want attached to
them) but being dynamic isn't a binary thing, it's an analog quality,
a language can be less or be more dynamic. For example I think Ruby is
more dynamic than Python, Python is more dynamic than CLisp, CLips
seems more dynamic than C#/Java, Java is more dynamic than D, and D is
more dynamic than C++. Often such differences aren't sharp, and you
can find ways to things more dynamically, even with a less nice syntax
(or creating less idiomatic code). (In C#4 they have even added a
dynamic feature that may make languages like IronPython/Boo faster and
simpler to write on the dotnet).
In the last two years I have seen many answers in the Python
newsgroups, and I have seen that some of the dynamic features of
Python aren't much used/appreciated:
- Metaclasses tricks
- exec and eval
- monkey patching done on classes
- arbitrary cmp among different types removed from Python 3
While some new static-looking/related features are being introduced:
- ABCs and function signatures added
- More tidy exception tree
So it seems that while C#/D are becoming more dynamic, Python/
JavaScript are becoming a little less dynamic looking (and this I
think this is positive, because too much dynamism turns code into a
swamp, and too much rigid systems lead to bloat and other problems.
Note that there are another orthogonal solution: to use an advanced
flexible and handy static type system, as in Haskell).
I have seen that in lot of those little programs of mine, or in a
significant percentage of their lines, I often don't use the dynamic
features of Python (this means that the same code can be written with
static types, especially if you can use templates like in C++/D, or a
flexible type system like in Haskell, and it also means that lot of
those small programs can be compiled by ShedSkin/Cython, with usually
a sharp decrease of running time).
What are the dynamic features of Python more used in your programs?
(From this set try to remove the things that can be done with a
flexible static template system, like the D one, that for some things
is more handy and powerful than the C++ template system, and for other
things less powerful).
If very little or no dynamic features are used in a program it may
seem a "waste" to use Python to write the code, because the final
program may be quite slow with no gain from the other features of
Python. (I think in such situations Python can be a good choice
anyway, because it's good to write working prototypes in a short
time). (The large number of solution of this page shows how a certain
class of Python programmers want more speed from their programs:
http://scipy.org/PerformancePython and note that page misses many
other solutions, like SIP, Boost Python, ShedSkin, Cinpy, Cython,
RPython, and so on).
In the last year I have found two situations where exec/eval is a way
to reduce a lot of the complexity of the code, so if used with care
the dynamic features can be quite useful.
Before ending this partially incoherent post, I'd also like to briefly
remind how the dynamic features are used in the Boo language: Boo
programs are generally statically typed, but duck types are used once
in a while to reduce the "pressure" of the static type system. You can
find more info on this on the Boo site. (Note that I have never seen a
good set of speed benchmarks to compare the performance of CPython to
Boo).
Bye,
bearophile
lines of code. For example to process some bioinformatics data,
perform some data munging, to apply a randomized optimization
algorithm to solve a certain messy problem, and many different things.
For that I often use several general modules that I have written, like
implementation of certain data structures, and small general "utility"
functions/classes, plus of course several external modules that I keep
updated.
Compared to other languages Python generally allows me to write a
correctly working program in the shorter time (probably because of the
Python shell, the built-in safeties, the doctests, the clean and short
and handy syntax, the quick write-run-test cycle, the logical design,
its uniformity, the availability of standard data structures and many
standard or external modules, and the low number (compared to other
languages) of corner cases and tricky semantic corners).
Today Python is defined a dynamic language (and not a scripting
language, a term that few languages today seem to want attached to
them) but being dynamic isn't a binary thing, it's an analog quality,
a language can be less or be more dynamic. For example I think Ruby is
more dynamic than Python, Python is more dynamic than CLisp, CLips
seems more dynamic than C#/Java, Java is more dynamic than D, and D is
more dynamic than C++. Often such differences aren't sharp, and you
can find ways to things more dynamically, even with a less nice syntax
(or creating less idiomatic code). (In C#4 they have even added a
dynamic feature that may make languages like IronPython/Boo faster and
simpler to write on the dotnet).
In the last two years I have seen many answers in the Python
newsgroups, and I have seen that some of the dynamic features of
Python aren't much used/appreciated:
- Metaclasses tricks
- exec and eval
- monkey patching done on classes
- arbitrary cmp among different types removed from Python 3
While some new static-looking/related features are being introduced:
- ABCs and function signatures added
- More tidy exception tree
So it seems that while C#/D are becoming more dynamic, Python/
JavaScript are becoming a little less dynamic looking (and this I
think this is positive, because too much dynamism turns code into a
swamp, and too much rigid systems lead to bloat and other problems.
Note that there are another orthogonal solution: to use an advanced
flexible and handy static type system, as in Haskell).
I have seen that in lot of those little programs of mine, or in a
significant percentage of their lines, I often don't use the dynamic
features of Python (this means that the same code can be written with
static types, especially if you can use templates like in C++/D, or a
flexible type system like in Haskell, and it also means that lot of
those small programs can be compiled by ShedSkin/Cython, with usually
a sharp decrease of running time).
What are the dynamic features of Python more used in your programs?
(From this set try to remove the things that can be done with a
flexible static template system, like the D one, that for some things
is more handy and powerful than the C++ template system, and for other
things less powerful).
If very little or no dynamic features are used in a program it may
seem a "waste" to use Python to write the code, because the final
program may be quite slow with no gain from the other features of
Python. (I think in such situations Python can be a good choice
anyway, because it's good to write working prototypes in a short
time). (The large number of solution of this page shows how a certain
class of Python programmers want more speed from their programs:
http://scipy.org/PerformancePython and note that page misses many
other solutions, like SIP, Boost Python, ShedSkin, Cinpy, Cython,
RPython, and so on).
In the last year I have found two situations where exec/eval is a way
to reduce a lot of the complexity of the code, so if used with care
the dynamic features can be quite useful.
Before ending this partially incoherent post, I'd also like to briefly
remind how the dynamic features are used in the Boo language: Boo
programs are generally statically typed, but duck types are used once
in a while to reduce the "pressure" of the static type system. You can
find more info on this on the Boo site. (Note that I have never seen a
good set of speed benchmarks to compare the performance of CPython to
Boo).
Bye,
bearophile