things I wish python could do

Scott David Daniels Scott.Daniels at Acm.Org
Wed May 12 18:33:30 EDT 2004


Ryan Paul wrote:
> I've spent a lot of time using python, and personally, I feel like it is
> vastly superior when compared to languages like java, and c++, but there
> are still a few things that detract from its elegance and flexibility.
> 
> 1. many keywords (eg:try/except) are strictly imperative, and cannot be
> used in a functional context.
Python is a language of statements and expressions.  This won't change.
If you dislike this feature, ml, lisp, (or it seems, ruby) will be more
to your liking.  Python has an imperative definition.  A pure functional
system has problems defining things like "print" which, by their very
nature, cause side-effects.

> this really should be possible: map(print,mylist)
After:
     def prints(*args):
         print ', '.join(map(str, args))
you can do:
     map(prints, mylist)
or even:
     prints(*mylist)

> 2. there is no easy way to extend existing classes externally.
> its possible, but it aint pretty...
> 
> class Shape:
>   def __init__(self,numSides, perimeter):
>     pass
> Currently you can do this:
> Shape.__dict__.update({
>   'triangle':ClassMethod(
>     lambda self,sideLength: Shape(3,sideLength*3))
>   
>   'square':ClassMethod(
>     lambda self,sideLength: Shape(4,sideLength*4))
> })
 > I want a way to extend a class as easily as I can define it.
 >

OK, this is just plain wrong.  You are walking the wrong way around a
building and then claiming, "these two doors are too far apart."

     class Shape:
         def __init__(self,numSides, perimeter):
             self.numSides = numSides
             self.perimeter = perimeter
             # which is, I expect what you meant)

     Shape.triangle = classmethod(lambda klass, side: klass(3, side*3))
     Shape.square = classmethod(lambda klass, side: klass(4, side*4))
and even:
     Shape.__repr__ = lambda self: 'Shape(%r, %r)' % (self.numSides,
                                                      self.perimeter)

Using klass above allows you to follow this code with:
      class FunnyShape(Shape):
           def avgSide(self):
               return float(self.perimeter) / self.numSides

where you can then:
       FunnyShape.triangle(5).avgSide()

> 3. you cant extend the builtins (string,list,etc), you have to inherit
> them. Guido says he wont let us because it would create compatability
> issues, and possibly break the interpreter. Maybe if we had namespace
> functionality, that wouldnt be an issue?
Nope, the complaint is about affecting the global environment
(thus making packages that you import unsure of the behavior of
the primitives).

> 4.  assignments cant be made inside of anonymous functions.
The idea is that generally anonymous functions should be trivial, or
you are better off separately defining them and assigning a meaningful
name to the code.  Such code takes more effort at the time you write it,
but the code becomes much more readable.

> I think most python programmers will agree, that python emphasizes
> simplicity, readability, and uniformity. 
 > Ultimately, I dont think that any of those things are important
 > enough  to justify the reduction of flexibility, syntactic
 > mutability, and versatility.
Then you really would prefer ruby or smalltalk.  If you are building
non-trivial, long-lived programs, you spend _far_ more time reading
code than writing it.  Flexibility is important, but there need not
be more than one way to do something to accommodate tastes.  Code
that mixes styles is the hardest kind of code to read (other than
machine-generated code).

I believe syntactic mutability is an idea that has been tried and
found wanting.  The more you can define non-standard syntax, the
more you have to know about a particular piece of code before you
can read it.

As for versatility, the pinnacle of versatility and flexibility
is machine code (not that wimpy assembler).  I have found very
few things I'd avoid doing in python, and most of those can be
build as extension modules fairly easily.

I've seen too many interlisp and smalltalk code sets that cannot
use each other's modules.  When you can modify the primitives
of a system for your convenience, it becomes tough to work with
someone else who doesn't share your particular quirks of how to
modify the base system.

After having written code for more than three decades, I find python
refreshing in having a language system with a clear discernible,
readable structure that I can trust.

Sorry for having taken troll-bait if that is what this post was.

-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list