Commonly-used names in the Python standard library

M

Marko Rauhamaa

Steven D'Aprano said:
And what a wonderful day that will be! Reading any piece of code you
didn't write yourself -- or wrote a long time ago -- will be an
adventure! Every script will have it's own exciting new set of
keywords doing who knows what, which makes every script nearly it's
own language! Oh joy, I cannot wait!

That's sarcasm, by the way.

I don't hear Lispers or C programmers complaining. Yes, you can shoot
yourself in the foot with macro trickery, but macros can greatly enhance
code readability and remove the need for code generators. That's why
they are used extensively in Linux kernel code and GOOPS (Guile's object
system), for example.
Then I can write code like:

for for in in:
while while:
if if:
raise raise

which will go a long way to ensuring that my code is an hostile and
unreadable as possible.

Perl does that by forcing you to prefix you variables with $ et al. The
analogy would be:

for $for in @in:
while $while:
if $if:
raise $raise

I'm not saying I like the look of that, but it does have a distinct
advantage in introducing new keywords.


Marko
 
C

Chris Angelico

I don't hear Lispers or C programmers complaining. Yes, you can shoot
yourself in the foot with macro trickery, but macros can greatly enhance
code readability and remove the need for code generators. That's why
they are used extensively in Linux kernel code and GOOPS (Guile's object
system), for example.

How does C let you create new keywords?

ChrisA
 
S

Steven D'Aprano

I don't hear Lispers or C programmers complaining.

Lisp is not a popular language. Despite being more powerful, more
efficient, and a lot older, I expect that there are far fewer people who
know Lisp (let alone use it regularly) than Python. I wouldn't go so far
as to say that the Lisp/Scheme family of languages is moribund, but they
are certainly niche.

And by the way, that niche includes some of the best and brightest
developers. (Some of whom are too clever by half, but that's another
story.) Merely mediocre programmers don't learn Lisp. So if you take the
smartest 0.1% of programmers, and give them a language that lets them
chainsaw their leg off, they won't complain, they're just keep their leg
out of the way. But if you take the average 50% programmers, and give
them a language that lets them chainsaw their leg off, there will be a
lot of one-legged programmers.

I'm really glad that Lisp exists, but I don't want Python to aspire to be
Lisp. In the same way, I am very fond of Forth. Forth too lets you re-
define *everything* about the language, including creating new flow-
control commands. I love that language. But there is a very good reason
why there are a thousand Python coders for every one Forth coder, and it
isn't the stack or the reverse Polish notation.

As for C, there are a lot of mediocre C programmers writing mediocre C
programs filled with buffer overflows, null-pointer bugs and all sorts of
other problems. And they don't complain about those either. Because the
smart ones know how not to chainsaw their leg off (but even they still
make mistakes, which is why there are periodic security vulnerabilities
even in code written by people of the calibre of Linus Torvalds and the
Linux kernel devs), or have moved to another language and write the bare
minimum of code in C only when they really need to.

Yes, you can shoot
yourself in the foot with macro trickery, but macros can greatly enhance
code readability

Or greatly destroy it, which is precisely the reason why Python doesn't
have a macro system. When Guido van Rossum reads Python code, he wants it
to look like Python code, not some arbitrary custom-built language.
 
M

Marko Rauhamaa

Chris Angelico said:
How does C let you create new keywords?

With #define. Nowhere near as elegant (flexible, hygienic) as in Lisp,
but used to create new syntax:

include/linux/list.h:
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)

include/linux/wait.h:
#define __wait_event_interruptible(wq, condition, ret) \
do { \
DEFINE_WAIT(__wait); \
\
for (;;) { \
prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
if (condition) \
break; \
if (!signal_pending(current)) { \
schedule(); \
continue; \
} \
ret = -ERESTARTSYS; \
break; \
} \
finish_wait(&wq, &__wait); \
} while (0)

In the latter example, note how "condition" is embedded in the middle of
the macro and evaluated repeatedly.


Marko
 
C

Chris Angelico

With #define. Nowhere near as elegant (flexible, hygienic) as in Lisp,
but used to create new syntax:

That can't create new syntax, though. All it can do is create a thing
that looks like a symbol or a function call and plonks a bit of code
in at that point. That's all. It's more akin to creating a function
that's able to work with blocks of unexecuted code.

ChrisA
 
M

Marko Rauhamaa

Chris Angelico said:
That can't create new syntax, though. All it can do is create a thing
that looks like a symbol or a function call and plonks a bit of code
in at that point. That's all. It's more akin to creating a function
that's able to work with blocks of unexecuted code.

Exactly! Lisp functions and macros (including special forms) are closely
related. The whole scheme (no pun intended) is so attractive because of
the S expression "supersyntax." A "while" macro is syntactically no
different from a "while" function. A macro is a "function" whose
automatic argument evaluation is disabled; the macro "function" gets the
ASTs of the arguments as input. A macro can always simulate a function
but not the other way round.

With the addition of macros, Python would become a (remote) Lisp
dialect. Defining macros would become more complicated because of
Python's more complex "supersyntax."


Marko
 
E

Ethan Furman

With the addition of macros, Python would become a (remote) Lisp
dialect. Defining macros would become more complicated because of
Python's more complex "supersyntax."

Have you tried MacroPy? If not, you should. If so, what were its failings?
 
8

88888 Dihedral

Chris Angelico <[email protected]>:










Shared C libraries face the exact same issue. Java seems pretty good on

this front as well. When there is a will, there is a way.





Marko

Well, please check jython first
under OS Andrioids( LINUX+JAVA-XWIN).
The mobile phone SW applications are getting hoter and hoter these days.
 

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
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top