Python's "only one way to do it" philosophy isn't good?

S

Steven D'Aprano

Lisp macros don't let you shoot yourself in the head -- only in the
foot. Being able to do

True = False

is being able to shoot yourself in the head. And Python certainly
lets you do that.

True, that is a current piece of badness. There are arguments for and
against making True and False reserved words, like None.

Nevertheless, in Python 1+2 always equals 3. You can't say the same thing
about Lisp.

The proof is in the pudding for anyone who has seen the advantages it
brings to Lisp. As Paul Graham points out, it's hard to look up and
see the advantages of what is up there in a more powerful language.
It's only easy to look down and see the disadvantages of what is missing
from a less powerful language. To understand the advantages, one has to
be willing to climb the hill and take in the view.

I don't accept Graham's reasoning.

I think that it is true that people's *values* change, as they get more
familiar with a more powerful language. For instance, I used to write
Pascal code on a Mac, and I avoided object-oriented techniques because
they were too inefficient and slow. (E.g. when Apple released System
7, which used OO code extensively, even the most one-eyed Mac fan boys
had to admit that the Finder ran like a dog compared to System 6.) But the
advantages were obvious, even before I had experience with OO design.

Now, I use Python, which is all object-oriented -- but the hardware I run
it on is so much faster that I care more about the programmer productivity
(my time) than about the efficiency of the executed code (the computer's
time). But if I were still running on a 1988-vintage Mac, you better believe I
wouldn't be making the same trade-offs I am now.

So, quite frankly, the things which you and Graham value in Lisp *simply
don't interest me*. It isn't that I can't see the advantages. They just
don't matter to me, in fact in some ways they are disadvantages.

Could my opinion ever change? Of course it could. I might, for example,
suddenly have a burning passion about language design, or find some
problem that is really painful under Python, and then I might go searching
for "a more powerful language". One doesn't need to be familiar with more
powerful languages to be aware that they offer benefits that less powerful
languages don't.

Or some really smart people might come up with a macro system that reduces
the disadvantages while keeping the advantages, and then dealing with
some random module developer's syntax extensions no more frightening than
dealing with some random module developer's functions.

(Considering the poor design and shoddy code of some modules I've had to
deal with, I really worry about giving developers the ability to redefine
syntax too.)

[snip]

They pay off in boatloads in the Lisp community.

But do they? Where is the standard library containing all these
magnificent macros? Forcing people to implement basic things from scratch
doesn't sound like a pay-off to me.

It seems that one of the major costs of having an extremely minimalist,
but easily extensible, syntax is that it encourages massive re-invention
of the wheel. Things get moved into the language slowly, because it is so
easy to work around the lack -- but the cost is a proliferation of
different macros all doing the same thing.

I think Python has the better philosophy. Python has one really good
syntax, minimalistic but not _overly_ minimalistic, not an easily
extensible syntax. The cost is that there are some things which could be
easier, if the syntax were different, but the benefit is that I don't have
to write syntax-extending macros, or debug them, or learn somebody else's.
Once every few years, Python introduces a new syntax, e.g. list
comprehensions, after much thought. That's better than every time I use a
different library or module.
 
L

Lenard Lindstrom

Douglas said:
When I said "without having to sully the code base", I meant that one
can implement a language feature for the target language as a loadable
module written entirely within the language itself, and without having
to understand anything particularly deep or specific about the language
implementation details.

I.e., I could write a new object system for Lisp faster than I could
even begin to fathom the internal of CPython. Not only that, I have
absolutely no desire to spend my valuable free time writing C code.
I'd much rather be hacking in Python, thank you very much.

CPython's class system is independent of the interpreter proper. That is
how two class systems, classic and new-style, can exist in the same
language. Both class types implement hooks into attribute lookup. By
providing __getattr__/__getattribute__, __setattr__, and __delattr__
methods one can effectively alter object behavior, such as method
resolution. And metaclasses change the nature of class statements. Novel
objects systems, such as prototypes, are possible. So the Python
language provides more of a framework for handling objects rather than
defining an actual object model. Python is the syntax; objects define
the behavior.

Finally, with the advent of Python 2.2, built-in types like int and list
became far more class like. Subclassing became possible. The object
types were reworking at the interpreter level. No amount of macro
wizardry could have done it.
PyPy sounds like a very interesting project indeed!

PyPy uses aspects without needing macros.
But I think that overall the problem of designing new syntax is more
in the design than the implementation. Anything new has to be
usable, readable, not clash too much with existing style, not
introduce ambiguities, and not move the extended language outside
the LL(1) [I believe that is right] subset of CFLs.

People (myself included) haven't had much trouble implementing nice
and useful macro packages for Lisp. Admittedly, it's a harder problem
for a language that doesn't have a Lisp-like syntax. I believe that
Dylan has macros without having a Lisp-like syntax, but Dylan is
really a dialect of Lisp, only with a more traditional Algol-like
syntax veneered onto it. My guess is that a macro developer for Dylan
would have to be familiar with an underlying hidden intermediate Lisp
syntax. (Though I'm just really just spouting that guess out of my
butt.)

Dylan macros do not use an "underlying hidden intermediate Lisp syntax".
They are language-based, doing pattern matching on Dylan language
elements ( http://www.opendylan.org/books/dpg/db_329.html ).
A few years back, I designed a somewhat Python-like language with a
macro facility for a class on dynamic languages and their
implementations. I didn't implement it, however, and I doubt that
I'll have time to get around to it in this lifetime.

It's easy to say Python would benefit from macros. Macros have solved
problems in Common Lisp and Scheme and so it is assumed they can do the
same for Python. But no concrete suggestions are made in this thread. No
specific macro mechanism is put forward for Python. No example is given
on how to implement some Python feature with it. No point has been
identified in the Python compiler chain for macro expansion.

When this thread turned to the topic of macros I did an Internet search
for information on macros relevant to Python. Dylan's macros look
promising. The Python-inspired language Converge has macros (
http://convergepl.org/ ). Michael Hudson's Bytecodehacks package
supports limited Python macros (
http://bytecodehacks.sourceforge.net/bch-docs/bch/module-bytecodehacks.macro.html
). There is also the __macro__ package, which I still have on my
computer, but I cannot find its home page.

The __macro__ package simply allows text substitution of source code at
module import time. The bytecodehack.macro module lets one define what
amounts to inlined functions. IMO neither package represents a
productive macro system. And I could find no other attempts to take
Python macros beyond wishful thinking. So until some solid proposal for
Python macros is put on the table any discussion of their merits is
unproductive. I can suggest though that procedural macros are a natural
starting point given the runtime nature of class and function creation.
 
S

Steven D'Aprano

You seem oblivious to the fact that one of the huge benefits of Python
is its elegant and readable syntax. The problem with not having a
"flexible syntax", is that a programming language can't provide
off-the-shelf an elegant syntax for all functionality that will ever
be needed.

It is hardly "off-the-shelf" if somebody has to create new syntax for it.


Eventually programmers find themselves in need of new
elegant functionality, but without a corresponding elegant syntax to
go along with the new functionality, the result is code that does not
look elegant and is therefore difficult to read and thus maintain.

That's true, as far as it goes, but I think you over-state your
case. The syntax included in Python is excellent for most things, and even
at its weakest, is still good. I can't think of any part of Python's
syntax that is out-and-out bad.

The reality is, one can go a long, long, long distance with Python's
syntax. Most requests for "new syntax" I've seen fall into a few
categories:

* optimization, e.g. case, repeat, multi-line lambda
* "language Foo looks like this, it is kewl"
* the usual braces/whitespace flamewars
* trying to get static type checking into the language


So let's be specific -- what do you think Python's syntax is missing? If
Python did have a macro facility, what would you change?

It is easy to point to functional features Python is missing, but are they
functions that can't be elegantly written without new syntax? Given
that not everybody is convinced that Python needs static type checking,
the only obvious hole I can see is the lack of syntax for
repeat-at-least-once.


[snip]
Take the issue up with Paul Graham. Since making a fortune developing
software in Lisp (making heavy use of macros), he now has much more
free time to write essays defending the truth than I do:

http://www.paulgraham.com/avg.html


I think the #1 advantage to using Lisp in a commercial enterprise is only
indirectly related to the language. Lisp coders are a self-selecting
above-average bunch -- no VB drones or C code monkeys need apply.

Graham talks about 25% of the Viaweb code base being macros. Imagine how
productive his coders would have been if the language was not quite
so minimalistic, so that they could do what they wanted without the
_lack_ of syntax getting in the way.
 
M

Michele Simionato

Functionality is no good if it's too cumbersome to use. For instance,
Scheme gives you first class continuations, which Python doesn't.
Continuations let you do *all sorts* of interesting things that you
just cannot do in Python. Like backtracking, for instance. (Well
maybe you could do backtracking in Python with lots of putting code
into strings and liberal use of eval, for all I know, but the results
would almost certainly be too much of a bear to actually use.)

Now, continuations, by themselves, in Scheme actually don't buy you
very much, because although they let you do some crazy powerful
things, making use of them to do so, is too confusing and verbose. In
order to actually use this very cool functionality, you need macros so
that you can wrap a pretty and easy-to-use face on top of all the
delicious continuation goodness.

You'll, just have to trust me on this one. I've written code with
continuations, and I just couldn't make heads or tails out of the code
a few hours later. But when prettied-up with a nice macro layer, they
can be a joy to behold.


Been there, done that. So what? Your example will not convince any
Pythonista.
The Pythonista expects Guido to do the language job and the
application developer
to do the application job. Consider for instance generators. In Python
they are
already implemented in the core language and the application developer
does not
care at all about implementing them. In Scheme I am supposed to
implement them myself with
continuations, but why should I do that, except as a learning
exercise? It is
much better if competent people are in charge of the very low level
stuff and
give me just the high level tools. I essentially view macros are low-
level performance
hacks, useful for language developers more than for application
developers.
BTW, there are already Python-like languages with macros (i.e. logix)
and still
nobody use them, including people with a Scheme/Lisp background. That
should be
telling you something.


Michele Simionato
 
M

Michele Simionato

The proof is in the pudding for anyone who has seen the advantages it
brings to Lisp. As Paul Graham points out, it's hard to look up and
see the advantages of what is up there in a more powerful language.
It's only easy to look down and see the disadvantages of what is
missing from a less powerful language. To understand the advantages,
one has to be willing to climb the hill and take in the view.

Right. However you fail to recognize that there are people here with a
good
understanding of Lisp and its macrology that still prefer Python over
Lisp.
I will go even further and say that the utility of macros is inversely
proportional
to the power of a language: the more the language is powerful, the
less macros
are useful. Really powerful languages (say Haskell, just not to be too
Python-centric)
do not need macros.

Provocative-but-with-a-grain-of-salt-in-it-yours,

Michele Simionato
 
M

Michele Simionato

When this thread turned to the topic of macros I did an Internet search
for information on macros relevant to Python. Dylan's macros look
promising. The Python-inspired language Converge has macros (http://convergepl.org/). Michael Hudson's Bytecodehacks package
supports limited Python macros (http://bytecodehacks.sourceforge.net/bch-docs/bch/module-bytecodehack...
). There is also the __macro__ package, which I still have on my
computer, but I cannot find its home page.

The __macro__ package simply allows text substitution of source code at
module import time. The bytecodehack.macro module lets one define what
amounts to inlined functions. IMO neither package represents a
productive macro system. And I could find no other attempts to take
Python macros beyond wishful thinking. So until some solid proposal for
Python macros is put on the table any discussion of their merits is
unproductive. I can suggest though that procedural macros are a natural
starting point given the runtime nature of class and function creation.

I would add to your list http://livelogix.net/logix/
and
http://www.fiber-space.de/EasyExtend/doc/main/EasyExtend.html

Michele Simionato
 
P

Paul Rubin

Michele Simionato said:
BTW, there are already Python-like languages with macros
(i.e. logix) and still nobody use them, including people with a
Scheme/Lisp background. That should be telling you something.

What about Dylan?
 
D

Douglas Alan

Steven D'Aprano said:
Nevertheless, in Python 1+2 always equals 3. You can't say the same thing
about Lisp.

Well, I can't say much of *anything* about "1 + 2" in Lisp, since
that's not the syntax for adding numbers in Lisp. In Lisp, numbers
are typically added using the "+" function, which might be invoked
like so:

(+ 1 2 3)

This would return 6.

It's true that some dialects of Lisp will let you redefine the "+"
function, which would typically be a bad idea. Other dialects would
give you an error or a warning if you tried to redefine "+". I would
fall more into the latter camp. (Though sometimes you might want a
way to escape such restrictions with some sort of "yes, I really want
to shoot myself in the head" declaration, as you may want to
experiment, not with changing the meaning of "(+ 1 2"), but rather
with adding some additional useful capability to the "+" function that
it doesn't already have.

Back on the Python front, although "1 + 2" might always equal 3 in
Python, this is really rather cold comfort, since no useful code would
ever do that. Useful code might include "a + 1", but since you can
overload operators in Python, you can say little about what "a + 1"
might do or mean on the basis of the syntax alone.

Furthermore, in Python you can redefine the "int" data type so that
int.__add__ does a subtraction instead. Then you end up with such
weirdness as
-1

Also, you can redefine the sum() function in Python.

So, we see that Python offers you a multitude of ways to shoot
yourself in the head.

One of the things that annoys me when coding in Python (and this is a
flaw that even lowly Perl has a good solution for), is that if you do
something like

longVarableName = foo(longVariableName)

You end up with a bug that can be very hard to track down. So one use
for macros would be so that I can define "let" and "set" statements so
that I might code like this:

let longVariableName = 0
set longVarableName = foo(longVariableName)

Then if longVarableName didn't already exist, an error would be
raised, rather than a new variable being automatically created for me.

The last time I mentioned this, Alex Martelli basically accused me of
being an idiot for having such trivial concerns. But, ya know -- it
isn't really a trivial concern, despite Martelli's obviously high
intellect. A woman I work with who is bringing up a CMS using Drupal
was complaining to me bitterly that this very same issue in PHP was
causing her bugs that were hard to track down. Unfortunately, I could
not gloat over her with my Python superiority, because if Drupal were
written in Python, rather than PHP, she'd have the very same problem
-- at least in this regard.

|>oug
 
D

Douglas Alan

Michele Simionato said:
Been there, done that. So what? Your example will not convince any
Pythonista.

I'm a Pythonista, and it convinces me.
The Pythonista expects Guido to do the language job and the
application developer to do the application job.

I'm happy to hear that there is a brain washing device built into
Python that provides all Python programmers with exactly the same
mindset, as that will certainly aid in having a consistent look and
feel to all Python code.
Consider for instance generators.

Yes, consider them! If Python had first class continuations (like
Ruby does) and macros in 1991, it could have had generators in 1992,
rather than in 2002. (I implemented generators using macros and stack
groups for Lisp Machines in 1983, and it took me all of a few hours.)
In Python they are already implemented in the core language and the
application developer does not care at all about implementing them.

And if they were implemented as macros in a library, then the
application developer doesn't have to care about implementing them
either.
In Scheme I am supposed to implement them myself with continuations,
but why should I do that, except as a learning exercise?

Well, when I get around to giving my sage advice to the Scheme
community, I'll let them know that generators need to be in the
standard library, not a roll-your-own exercise.
It is much better if competent people are in charge of the very low
level stuff and give me just the high level tools.

Even many competent people don't want to hack in the implementation
language and have to understand the language implementation internals
to design and implement language features. By your argument,
Pythonistas might as well insist that the entire standard library be
coded in C.
BTW, there are already Python-like languages with macros
(i.e. logix) and still nobody use them, including people with a
Scheme/Lisp background. That /should be telling you something.

It only tells me what I've known for at least a couple decades now --
that languages live and die on issues that often have little to do
with the language's intrinsic merits.

|>oug
 
S

Steven D'Aprano

One of the things that annoys me when coding in Python (and this is a
flaw that even lowly Perl has a good solution for), is that if you do
something like

longVarableName = foo(longVariableName)

You end up with a bug that can be very hard to track down. So one use
for macros would be so that I can define "let" and "set" statements so
that I might code like this:

let longVariableName = 0
set longVarableName = foo(longVariableName)

Then if longVarableName didn't already exist, an error would be
raised, rather than a new variable being automatically created for me.

So "let" is the initial declaration, and "set" modifies the existing
variable?

What happens is you declare a variable twice?

let longVariableName = 0
let longVariableName = foo(longVariableName) # oops I meant set

How long did it take you to write the macros, and use them, compared to
running Pylint or Pychecker or equivalent?


But if you really want declarations, you can have them.
import variables
variables.declare(x=1, y=2.5, z=[1, 2, 4])
variables.x = None
variables.w = 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "variables.py", line 15, in __setattr__
raise self.DeclarationError("Variable '%s' not declared" % name)
variables.DeclarationError: Variable 'w' not declared



The variables module isn't part of the standard library. Here's the code
for it:

import sys

class _variable:
class DeclarationError(TypeError): pass
def __setattr__(self, name, value):
if self.__dict__.has_key(name):
self.__dict__[name] = value
else:
raise self.DeclarationError("Variable '%s' not declared" % name)
self.__dict__[name]=value
def declare(self, **args):
for name, value in args.items():
self.__dict__[name] = value

sys.modules[__name__]=_variable()


It took me less than five minutes starting from Alex Martelli's code here
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65207

No special syntax or macros or magic powers were needed. I could extend
the variables functionality to (say) make sure the same variable isn't
declared twice, or be case-insensitive, or have multiple namespaces, none
of which need special syntax.
 
D

Douglas Alan

So "let" is the initial declaration, and "set" modifies the existing
variable?
Yes.

What happens is you declare a variable twice?

The same thing that would happen in Perl or any other language that
supports this type of variable declaration and setting: it would raise
an error.

The big debate you get next, is then whether you should be allowed to
shadow variables in nested scopes with new variables of the same
name. Given that Python already allows this, my guess is that the
answer should be yes.
How long did it take you to write the macros, and use them, compared
to running Pylint or Pychecker or equivalent?

An hour? Who cares? You write it once and then you have it for the
rest of your life. You put it in a widely available library, and then
*every* programmer also has it for the rest of their lives. The
amortized cost: $0.00. The value: priceless.
But if you really want declarations, you can have them.
import variables
variables.declare(x=1, y=2.5, z=[1, 2, 4])
variables.x = None
variables.w = 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "variables.py", line 15, in __setattr__
raise self.DeclarationError("Variable '%s' not declared" % name)
variables.DeclarationError: Variable 'w' not declared

Thanks, but that's just too syntactically ugly and verbose for me to
use. Not only that, but my fellow Python programmers would be sure to
come and shoot me if I were to code that way.

One of the reasons that I want to use Python is because I like reading
and writing code that is easy to read and looks good. I don't want to
bend it to my will at the expense of ugly looking code.

|>oug
 
D

Douglas Alan

Steven D'Aprano said:
But if you really want declarations, you can have them.
import variables
variables.declare(x=1, y=2.5, z=[1, 2, 4])
variables.x = None
variables.w = 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "variables.py", line 15, in __setattr__
raise self.DeclarationError("Variable '%s' not declared" % name)
variables.DeclarationError: Variable 'w' not declared

Oh, I forgot to mention that I work a lot on preexisting code, which I
am surely not going to go to all the effort to retype and then retest.
With the "let" and "set" macros I can use "set" without a matching
"let". "set" just checks to make sure that a variable already exists
before assigning to it, and "let" just prevents against
double-declarations. They can be used independently or together.
With your "variables" class, they have to be used together.

|>oug
 
S

Steven D'Aprano

An hour? Who cares? You write it once and then you have it for the
rest of your life. You put it in a widely available library, and then
*every* programmer also has it for the rest of their lives. The
amortized cost: $0.00. The value: priceless.

Really? Where do I download this macro? How do I find out about it? How
many Lisp programmers are using it now?

How does your glib response jib with your earlier claims that the
weakness of Lisp/Scheme is the lack of good libraries?

Googling for ' "Douglas Allen" download lisp OR scheme ' wasn't very
promising. If you have made your macros available to others, they don't
seem to be very well-known.

In fairness, the various Python lints/checkers aren't part of the standard
library either, but they are well-know "standards".


But if you really want declarations, you can have them.
import variables
variables.declare(x=1, y=2.5, z=[1, 2, 4])
variables.x = None
variables.w = 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "variables.py", line 15, in __setattr__
raise self.DeclarationError("Variable '%s' not declared" % name)
variables.DeclarationError: Variable 'w' not declared

Thanks, but that's just too syntactically ugly and verbose for me to
use.


"Syntactically ugly"? "Verbose"?

Compare yours with mine:

let x = 0
let y = 1
let z = 2
set x = 99

(Looks like BASIC, circa 1979.)

variables.declare(x=0, y=1, z=2)
variables.x = 99

(Standard Python syntax.)

I don't think having two easily confused names, let and set, is an
advantage, but if you don't like the word "declare" you could change it to
"let", or change the name of the module to "set" (although that runs the
risk of confusing it with sets).

Because this uses perfectly stock-standard Python syntax, you could even
do this, so you type fewer characters:

v = variables
v.x = 99

and it would Just Work.

Not only that, but my fellow Python programmers would be sure to
come and shoot me if I were to code that way.

*shrug* They'd shoot you if you used "let x = 0" too.

One of the reasons that I want to use Python is because I like reading
and writing code that is easy to read and looks good. I don't want to
bend it to my will at the expense of ugly looking code.

But the "ugly looking code" is stock-standard Python syntax.

module.function(keyword=value)
module.attribute = value

is precisely the standard Python syntax you describe as "easy to read and
looks good" one moment. I don't believe you that you find it "ugly looking
code" -- if you did, you wouldn't be using Python.
 
D

Douglas Alan

Really? Where do I download this macro? How do I find out about it? How
many Lisp programmers are using it now?

(1) I didn't have to write such a macro for Lisp, as Lisp works
differently. For one thing, Lisp already has let and set special
forms. (Lisp uses the term "special form" for what Python would call
a "statement", but Lisp doesn't call them statements since they return
values.)

(2) You act as if I have no heavy criticisms of Lisp or the Lisp
community. I critique everything with equal vigor, and keep an eye
out for the good aspects and ideas of everything with equal vigor.
How does your glib response jib with your earlier claims that the
weakness of Lisp/Scheme is the lack of good libraries?

(1) See above. (2) My response wasn't glib.
Googling for ' "Douglas Allen" download lisp OR scheme ' wasn't very
promising.

(1) You spelled my name wrong. (2) I haven't written any libraries
for any mainstream dialects of Lisp since there was a web. I did
write a multiple dispatch lookup cacher for a research dialect of
Lisp, but it was just an exercise for a version of Lisp that few
people have ever used.
In fairness, the various Python lints/checkers aren't part of the standard
library either, but they are well-know "standards".

In general I don't like such checkers, as I tend to find them more
annoying than useful.
"Syntactically ugly"? "Verbose"?
Compare yours with mine:
let x = 0
let y = 1
let z = 2
set x = 99
(Looks like BASIC, circa 1979.)

It looks like a lot of languages. And there's a reason for that -- it
was a good idea.
variables.declare(x=0, y=1, z=2)
variables.x = 99
(Standard Python syntax.)
I don't think having two easily confused names, let and set is an
advantage,

Let and set are not easily confused. Lisp programmers have had
absolutely no problem keeping the distinction separate for the last 47
years now.
but if you don't like the word "declare" you could change it to
"let", or change the name of the module to "set" (although that runs the
risk of confusing it with sets).
Because this uses perfectly stock-standard Python syntax, you could even
do this, so you type fewer characters:
v = variables
v.x = 99
and it would Just Work.

I wouldn't program that way, and no one that I know would either.

In this regard you sound exactly like all the C++ folks, who when you
point out that something in C++ is inadequate for one's needs, they
point you at some cumbersome and ugly solution and then tell you that
since C++ can already deal with the complaint, that there's no good
reason to consider changing C++. Consequently, C++ still doesn't have
a "finally" statement, and it requires either making instance
variables public or forcing the programmer to write lots of
boilerplate code writing setter and getter functions. Fortunately,
the Python developers finally saw the errors of their ways in this
regard and fixed the situation. But, it seems to me that you would
have been one of those people saying that there's no need to have a
way of overriding attribute assignment and fetching, as you can always
just write all that extra boilerplate code, or instead add an extra
layer of indirection (proxy objects) in your instance data to have
things done the way you want, at the expense of ugly code.
*shrug* They'd shoot you if you used "let x = 0" too.

Clearly you are not familiar with the programmers that I work with.
As I mentioned previously, at least one of them is quite upset about
the auto-declaration feature of most scripting languages, and your
suggestion would not make her any happier.
But the "ugly looking code" is stock-standard Python syntax.

There many things that cannot be done in stock Python syntax elegantly
(e.g. multiple predicate dispatch), which is why, when programming in
Python, one often sticks to doing things the way that *can* be done
elegantly. (This can often result in programs that are structured
less elegantly in the large, however.) If you don't recognize this,
then you must be livid over the addition to Python of decorators, list
and generator comprehension, etc. After, all, Python is Turing
complete, and any problem that can be solved in Python now could have
been solved in Python before. The solution might just have looked a
little (or a lot) different.
module.function(keyword=value)
module.attribute = value
is precisely the standard Python syntax you describe as "easy to read and
looks good" one moment. I don't believe you that you find it "ugly looking
code" -- if you did, you wouldn't be using Python.

It's ugly for the purpose of local variable assignment, as it doesn't
*look* like local variable assignment. If I wanted to program in a
language that didn't understand the idea that different sorts of
things ought to look different, I'd program in Java, rather than Python.

|>oug
 
R

Robert Brown

Steven D'Aprano said:
Graham talks about 25% of the Viaweb code base being macros. Imagine how
productive his coders would have been if the language was not quite
so minimalistic, so that they could do what they wanted without the
_lack_ of syntax getting in the way.

Paul Graham's Viaweb code was written in Common Lisp, which is the least
minimalistic dialect of Lisp that I know. Even though they were using this
powerful tool, they still found it useful to create new syntactic
abstractions. How much less productive would they have been had they not
had this opportunity?
 
D

Douglas Alan

It is hardly "off-the-shelf" if somebody has to create new syntax
for it.

Ummm. that's my point. No language can provide all the syntax that
will ever be needed to write elegant code. If module authors can
provide the syntax needed to use their module elegantly, then problem
solved.
That's true, as far as it goes, but I think you over-state your
case.

I do not.

It is so easy for you, without *any* experience with a language (i.e.,
Lisp) or its community to completely dismiss the knowledge and wisdom
acquired by that community. Doesn't that disturb you a bit?
The syntax included in Python is excellent for most things, and even
at its weakest, is still good. I can't think of any part of Python's
syntax that is out-and-out bad.

The proposed syntax for using the proposed predicate-based multimethod
library is ungainly.

Until decorators were added to the language, the way to do things that
decorators are good for was ugly. Decorators patch up one ugliness,
but who wants Python to become an old boat with lots of patches?

Nearly every addition made to Python since 1.5 could have been done in
the standard library, rather than being made to the core language, if
Python had a good macro system. The exceptions, I think, being
objects all the way down, and generators. Though generators could
have been done in the standard library too, if Python had first class
continuations, like Scheme and Ruby do.

Over time, an infinite number of examples will turn up like this, and
I claim (1) that it is better to modify the standard library than to
modify the language implementation, and that (2) it is better to allow
people to experiment with language features without having to modify
the implementation, and (3) that it is better to allow people to
distribute new language features for experimentation or production in
a loadable modular fashion, and (4) that it is better to allow
application developers to develope new language features for their
application frameworks than to not.
The reality is, one can go a long, long, long distance with Python's
syntax.

And you can go a long, long way with Basic, or Fortran, or C, or C++,
or Haskell, or Lisp. None of this implies that there aren't
deficiencies in all of these languages. Python is no exception.
Python just happens to be better than most in a number of significant
regards.
Most requests for "new syntax" I've seen fall into a few
categories:
* optimization, e.g. case, repeat, multi-line lambda

I don't give a hoot about case or repeat, though a Lisp-like "loop
macro" might be nice. (The loop macro is a little mini language
optimized for coding complicated loops.) A multi-line lambda would
be very nice.
* "language Foo looks like this, it is kewl"

Sometimes language Foo has features that are actually important to for
a specific application or problem domain. It's no accident, for
instance, that Lisp is still the preferred language for doing AI
research. It's better for Python if Python can accommodate these
applications and domains than for Python to give up these markets to
Foo.
* the usual braces/whitespace flamewars
* trying to get static type checking into the language


So let's be specific -- what do you think Python's syntax is missing? If
Python did have a macro facility, what would you change?

In addition to the examples given above, symbols would be nice. Lisp
has 'em, Ruby has 'em, Python doesn't. They are very useful.

An elegant multimethod based object system will be essential
for every language someday, when the time is right for people to
understand the advantages.

Manifest typing will be essential.

A backtracking system is important for some applications. Perhaps all
applications, someday.

The ability to make mini-languages for specific domains, like fields
of math and science, is very useful, so the mathematicians and
scientists can denote things in a notation that is closer to the
notation that they actually work in.

Etc., etc., etc. The future is long, and our ability to peer into it
is blurry, and languages that can adapt to the unforeseen needs of that
blurry future are the ones that will survive.

For instance, I can state with almost 100% certainty that one hundred
years from now, some dialect of Lisp will still be around and in
common usage. I can't say the same thing about Python. I can't say
that about Python ten years from now with Ruby popularity now hot on
the heels of Python's.
I think the #1 advantage to using Lisp in a commercial enterprise is
only indirectly related to the language. Lisp coders are a
self-selecting above-average bunch -- no VB drones or C code monkeys
need apply.

And it's self-selecting because the people don't want to live within
the limits of VB or C or perhaps even Python. Why should Python want
to alienate these uber-hackers rather than attract them?
Graham talks about 25% of the Viaweb code base being macros. Imagine how
productive his coders would have been if the language was not quite
so minimalistic, so that they could do what they wanted without the
_lack_ of syntax getting in the way.

You just prove Graham's point--that one is not qualified to judge the
features of a more powerful language without actually learning and
using the language--when you say things like this. Viaweb almost
certainly used Common Lisp, not Scheme, and Common Lisp is anything
but a minimalistic language. Python looks like a minimalistic
language compared to Common Lisp.

*Scheme* is the most well-known minimalistic Lisp, and was created, in
part, as a response to the the feature-creep of Common Lisp. (Or
actually to the immediate precursors of Common Lisp.) But few people
use Scheme for real work (as opposed to academic work). Common Lisp
is the production Lisp work horse.

(The reasons for Common Lisp being dominant for production are as
complex as the reasons that other languages win or lose in market
place, but they have something to do with there being very efficient
implementations of Common Lisp, people actually wanting all those
creeping features, and the Scheme standardizing committees operating
so slowly.)

|>oug
 
G

Graham Breed

Steven D'Aprano wote:
But if you really want declarations, you can have them.
import variables
variables.declare(x=1, y=2.5, z=[1, 2, 4])
variables.x = None
variables.w = 0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "variables.py", line 15, in __setattr__
raise self.DeclarationError("Variable '%s' not declared" % name)
variables.DeclarationError: Variable 'w' not declared

Another way is to decorate functions with their local variables:
.... def f(x=1, y=2.5, z=[1,2,4]):
.... x = float(x)
.... w = float(y)
.... return [item+x-y for item in z]
....
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "strict.py", line 11, in dec
raise DeclarationError("No slot for %s"%varname)
strict.DeclarationError: No slot for w

and the implementation

import re

class DeclarationError(TypeError): pass

def my(slots=""):
tokens = slots.split()
def dec(func):
code = func.func_code
for varname in code.co_varnames[code.co_argcount:]:
if re.match('\w+$', varname) and varname not in tokens:
raise DeclarationError("No slot for %s"%varname)
return func
return dec


The best way to catch false rebindings is to stick a comment with the
word "rebound" after every statement where you think you're rebinding
a variable. Then you can search your code for cases where there's a
"rebound" comment but no rebinding. Assuming you're the kind of
person who knows that false rebindings can lead to perplexing bugs,
but doesn't check apparent rebindings in a paranoid way every time a
perplexing bug comes up, anyway. (They aren't that common in modern
python code, after all.) And that you remembered to add the comments
(like you would have remembered the let and set). And you're also the
kind of person who's troubled by perplexing bugs but doesn't run a
fully fledged lint. Maybe that's the kind of person who wouldn't put
up with anything short of a macro as in the original proposal. All I
know is that it's the kind of person I don't want to second guess.


Graham
 
D

Douglas Alan

Graham Breed said:
Another way is to decorate functions with their local variables: ... def f(x=1, y=2.5, z=[1,2,4]):
... x = float(x)
... w = float(y)
... return [item+x-y for item in z]

Well, I suppose that's a bit better than the previous suggestion, but
(1) it breaks the style rule of not declaring variables until you need
them, and (2) it doesn't catch double initialization.
The best way to catch false rebindings is to stick a comment with
the word "rebound" after every statement where you think you're
rebinding a variable.

No, the best way to catch false rebindings is to have the computers
catch such errors for you. That's what you pay them for.
Then you can search your code for cases where there's a "rebound"
comment but no rebinding.

And how do I easily do that? And how do I know if I even need to in
the face of sometimes subtle bugs?
Assuming you're the kind of person who knows that false rebindings
can lead to perplexing bugs, but doesn't check apparent rebindings
in a paranoid way every time a perplexing bug comes up, anyway.
(They aren't that common in modern python code, after all.)

They're not that uncommon, either.

I've certainly had it happen to me on several occasions, and sometimes
they've been hard to find as I might not even see the mispeling even
if I read the code 20 times.

(Like the time I spent all day trying to figure out why my assembly
code wasn't working when I was a student and finally I decided to ask
the TA for help, and while talking him through my code so that he
could tell me what I was doing wrong, I finally noticed the "rO" where
there was supposed to be an "r0". It's amazing how useful a TA can
be, while doing nothing at all!)
And you're also the kind of person who's troubled by perplexing bugs
but doesn't run a fully fledged lint.

Maybe PyLint is better than Lint for C was (hated it!), but my idea of
RAD does not include wading through piles of useless warning messages
looking for the needle warning in the warning haystack. Or running
any other programs in the midst of my code, run, code, run, ..., loop.
Maybe that's the kind of person who wouldn't put up with anything
short of a macro as in the original proposal. All I know is that
it's the kind of person I don't want to second guess.

As it is, I code in Python the way that a normal Python programmer
would, and when I have a bug, I track it down through sometimes
painstaking debugging as a normal Python programmer would. Just as
any other normal Python programmer, I would not use the alternatives
suggested so far, as I'd find them cumbersome and inelegant. I'd
prefer not to have been bit by the bugs to begin with. Consequently,
I'd use let and set statements, if they were provided (or if I could
implement them), just as I have the equivalents to let and set in
every other programming language that I commonly program in other than
Python.

|>oug
 
M

Michele Simionato

One of the things that annoys me when coding in Python (and this is a
flaw that even lowly Perl has a good solution for), is that if you do
something like

longVarableName = foo(longVariableName)

You end up with a bug that can be very hard to track down.

You should really be using pychecker (as well as Emacs autocompletion
feature ...):

~$ cat x.py
def foo(x): return x

longVariableName = 1
longVarableName = foo(longVariableName)

~$ pychecker -v x.py
Processing x...

Warnings...

x.py:4: Variable (longVarableName) not used

[I know you will not be satisfied with this, but pychecker is really
useful,
since it catches many other errors that no amount of macroprogramming
would
evere remove].

Michele Simionato
 

Members online

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,329
Latest member
InezZ76898

Latest Threads

Top