Dynamic features used

Aaron Brady castironpi at gmail.com
Sat Nov 22 05:58:13 EST 2008


On Nov 21, 3:17 am, bearophileH... at lycos.com wrote:
snip
> 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).

I like the brevity (conciseness).  The Python implementation of a
program is shorter on average than the C++ implementation.  You don't
have to declare your types, which is just extra words and handcuffs,
looking at everything but performance.  Some of the things that C++
can do are irrelevant to some programs, and Python keeps them off your
mind.  C++ gives you much finer control of a machine, but you don't
always need it.

I think the central decision-making has the advantage that there is
only one Python, whereas C has many versions.  It makes it easier to
share and communicate about programs in the language.  Plus I don't
even need new files to cut-and-paste a snippet from the newsgroup into
the interpreter.

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

Python can be written in C.  Thus, anything Python can do, C can do,
not to mention the equal expressiveness of the languages.  But I have
a non-trivial observation.

With an STL 'map' and a union, you could make a quick and dirty quasi-
Python in C++.  Dynamic assignment is just a __dict__ member anyway.

Functions (and classes) are first-class objects, but the 'functor'
pattern can approximate it, even including decorators.  Variable-
length arguments are just an STL 'vector', and keyword arguments are
just an STL dict.  You can't get the same flexibility in C++ though,
without parsing the text of a function, and storing argument info
about it.  Syntax can't leave the realm of C++, but just a few classes
could be able to bring C++ a lot closer to Python.

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

You didn't mention 'metaprogramming', which is writing a program to
generate the text code of a program, which you then compile/
interpret.  I think 'exec' enables you to keep such a program in one
piece.  For a rough example, a C++ program might have a dozen
repetitive classes, which you would have to write by hand or generate
externally.  The Python program can just execute the definitions in
place.  That is, if 'setattr' doesn't already do the trick.

The ability to return a class or a function obsoletes some of the
Gamma et al Design Patterns, such as the Factory pattern, IIRC.

And you didn't mention garbage collection.

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

Some of the value of that comes from the instant gratification that
Python gives one.  'Now' is a good time to test your code.  'Later' is
ok.  I'm thinking of writing graphics with pygame, which lacks the
same quality in C++.  Python is eloquent, whereas I'd feel I'd just
beaten a dead horse by the time I got the C++ equivalents up and
running, though perhaps less so with practice.

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

I'm not sure how much of my like of Python comes from the pioneer's
spirit, the feeling that it's new and hot and secret, undiscovered
country, like I'm out in the wilderness.  But nifty tricks such as
with decorators are part of the fun.  The coolest two lines of Python
are pretty cool.



More information about the Python-list mailing list