things I wish python could do

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan Paul

    things I wish python could do

    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. I
    thought I might mention a few of them. I'd like to hear what people think
    of my complaints, and I also like to hear the complaints of others.

    1. many keywords (eg:try/except) are strictly imperative, and cannot be
    used in a functional context.

    this really should be possible: map(print,mylis t)

    2. there is no easy way to extend existing classes externally.

    its possible, but it aint pretty...

    class Shape:
    def __init__(self,n umSides, perimeter):
    pass

    Currently you can do this:

    Shape.__dict__. update({
    'triangle':Clas sMethod(
    lambda self,sideLength : Shape(3,sideLen gth*3))

    'square':ClassM ethod(
    lambda self,sideLength : Shape(4,sideLen gth*4))
    })

    I want a way to extend a class as easily as I can define it.

    3. you cant extend the builtins (string,list,et c), 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?

    4. assignments cant be made inside of anonymous functions.

    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. I was surprised to
    find that Ruby solves all of my python complaints, while achieving
    grace and simplicity easily comparable to that of python, and providing a
    few innovative extras (including real private, public, and protected
    methods).

    Despite the fact that I disagree with quite a bit of the philosophy behind
    prothon, I have enjoyed watching it, because it inspires good dialogue
    about language development. (as long as those with closed minds keep their
    mouths closed as well)

    Are there are any strong reasons why a language shouldn't support the
    things I list? Is anybody aware of features promised for future python
    versions that solve or nullify my problems? Are any of my perceived
    problems really just products of ignorance?
  • Scott David Daniels

    #2
    Re: things I wish python could do

    Ryan Paul wrote:[color=blue]
    > 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.[/color]
    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.
    [color=blue]
    > this really should be possible: map(print,mylis t)[/color]
    After:
    def prints(*args):
    print ', '.join(map(str, args))
    you can do:
    map(prints, mylist)
    or even:
    prints(*mylist)
    [color=blue]
    > 2. there is no easy way to extend existing classes externally.
    > its possible, but it aint pretty...
    >
    > class Shape:
    > def __init__(self,n umSides, perimeter):
    > pass
    > Currently you can do this:
    > Shape.__dict__. update({
    > 'triangle':Clas sMethod(
    > lambda self,sideLength : Shape(3,sideLen gth*3))
    >
    > 'square':ClassM ethod(
    > lambda self,sideLength : Shape(4,sideLen gth*4))
    > })
    > I want a way to extend a class as easily as I can define it.
    >[/color]

    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,n umSides, perimeter):
    self.numSides = numSides
    self.perimeter = perimeter
    # which is, I expect what you meant)

    Shape.triangle = classmethod(lam bda klass, side: klass(3, side*3))
    Shape.square = classmethod(lam bda 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(Shap e):
    def avgSide(self):
    return float(self.peri meter) / self.numSides

    where you can then:
    FunnyShape.tria ngle(5).avgSide ()
    [color=blue]
    > 3. you cant extend the builtins (string,list,et c), 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?[/color]
    Nope, the complaint is about affecting the global environment
    (thus making packages that you import unsure of the behavior of
    the primitives).
    [color=blue]
    > 4. assignments cant be made inside of anonymous functions.[/color]
    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.
    [color=blue]
    > 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.[/color]
    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@A cm.Org

    Comment

    • Peter Maas

      #3
      Re: things I wish python could do

      Ryan Paul wrote:[color=blue]
      > 1. many keywords (eg:try/except) are strictly imperative, and cannot be
      > used in a functional context.
      >
      > this really should be possible: map(print,mylis t)[/color]

      To make everything functional in Python would probably be a
      huge effort with cosmetic effects only. What about defining
      e.g.

      def fprint(item):
      print item

      putting all functional definitions in a module 'functional'
      and using it:

      from functional import fprint

      ....
      map(fprint, mylist)

      Mit freundlichen Gruessen,

      Peter Maas

      --
      -------------------------------------------------------------------
      Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
      Tel +49-241-93878-0 Fax +49-241-93878-20 eMail peter.maas@mplu sr.de
      -------------------------------------------------------------------

      Comment

      • Jacek Generowicz

        #4
        Re: things I wish python could do

        Ryan Paul <segphault@sbcg lobal.net> writes:
        [color=blue]
        > 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. I
        > thought I might mention a few of them. I'd like to hear what people think
        > of my complaints, and I also like to hear the complaints of others.
        >
        > 1. many keywords (eg:try/except) are strictly imperative, and cannot be
        > used in a functional context.[/color]

        Yes, I find the whole statement expression dichotomy one huge PITA.
        [color=blue]
        > this really should be possible: map(print,mylis t)
        >
        > 2. there is no easy way to extend existing classes externally.
        >
        > its possible, but it aint pretty...
        >
        > class Shape:
        > def __init__(self,n umSides, perimeter):
        > pass
        >
        > Currently you can do this:
        >
        > Shape.__dict__. update({
        > 'triangle':Clas sMethod(
        > lambda self,sideLength : Shape(3,sideLen gth*3))
        >
        > 'square':ClassM ethod(
        > lambda self,sideLength : Shape(4,sideLen gth*4))
        > })[/color]

        Which looks like (I don't know what ClassMethod is) a long-winded way
        of wrting:

        Shape.triangle = lambda self, sideLength: Shape(...))
        [color=blue]
        > I want a way to extend a class as easily as I can define it.[/color]

        You can. There is one syntax for creating new classes, and another for
        mutating existing ones. Ruby conflates the two.

        I think that what you "want" is to cleate new classes and mutate
        existing ones using the same syntax. Perhaps you should question
        whether you really want that.

        Maybe there's a satisfactory solution based on the decorators that are
        being proposed in PEP 318:

        class Shape [update]:
        def triangle(self, sideLength):
        ...
        [color=blue]
        > 4. assignments cant be made inside of anonymous functions.[/color]

        Actually, there's very little you can do in anonymous functions, given
        that you are restricted to a _single expression_.
        [color=blue]
        > I was surprised to find that Ruby solves all of my python
        > complaints, while achieving grace and simplicity easily comparable
        > to that of python, and providing a few innovative extras (including
        > real private, public, and protected methods).[/color]

        Yes, access-restricted private methods were an innovation in the field
        of programmer productivity reduction, but it was not an innovation on
        Ruby's part ... though why a language which sounds quite sensible,
        would choose to embrace such a criminal practice is a mystery to me.

        (Hmm ... briefly perusing a Ruby manual, I get the impression that
        some of it is a result of having to get around problems caused by all
        classes inheriting all (would-be) global functions ... which makes me
        reconsider my statement that Ruby sounds quite sensible.)
        [color=blue]
        > Are there are any strong reasons why a language shouldn't support the
        > things I list?[/color]

        1) Keywords are just plain wrong.

        4) Python's indentation-based block structure makes general anonymous
        functions a bit problematic. (Briefly perusing the PEPs I notice 308
        ... and I'm surprised that it's publication date is not April 1st,
        as it looks like a PERLification attempt.)
        [color=blue]
        > Is anybody aware of features promised for future python versions
        > that solve or nullify my problems?[/color]

        Read the PEPs.
        [color=blue]
        > Are any of my perceived problems really just products of ignorance?[/color]

        Probably.

        I'm feeling in a controversy-stirring mood. Can you tell?

        With any luck I've trodden on Python's, Ruby's and your toes :-)

        Comment

        • Andrew Bennetts

          #5
          Re: things I wish python could do

          On Thu, May 13, 2004 at 10:07:19AM +0200, Jacek Generowicz wrote:
          [...][color=blue]
          >[color=green]
          > > I want a way to extend a class as easily as I can define it.[/color]
          >
          > You can. There is one syntax for creating new classes, and another for
          > mutating existing ones. Ruby conflates the two.
          >
          > I think that what you "want" is to cleate new classes and mutate
          > existing ones using the same syntax. Perhaps you should question
          > whether you really want that.
          >
          > Maybe there's a satisfactory solution based on the decorators that are
          > being proposed in PEP 318:
          >
          > class Shape [update]:
          > def triangle(self, sideLength):
          > ...[/color]

          Or there's the evil solution, available right now:


          -Andrew.


          Comment

          • Terry Reedy

            #6
            Re: things I wish python could do


            "Jacek Generowicz" <jacek.generowi cz@cern.ch> wrote in message
            news:tyfk6zgpx6 w.fsf@pcepsft00 1.cern.ch...[color=blue]
            > Ryan Paul <segphault@sbcg lobal.net> writes:[/color]
            [color=blue]
            > Yes, I find the whole statement expression dichotomy one huge PITA.[/color]

            Would you really prefer writing <target> = expression as set('<target>',
            expression) or as the 'special-form' pseudofunction call setq(<target>,
            expression)? Just about all Python statements are statements because they
            would also be a PITA, and possibly more so, as expressions.
            [color=blue][color=green]
            > > 4. assignments cant be made inside of anonymous functions.[/color]
            >
            > Actually, there's very little you can do in anonymous functions, given
            > that you are restricted to a _single expression_.[/color]

            Lambda expressions in Python are strictly an abbreviation for functions
            with a trivial body ('return expression'), nothing more. Anonymity is a
            wart, not a feature. The keyword is, to me, a mistake because of the false
            expectations it stimulates.
            [color=blue]
            > I'm feeling in a controversy-stirring mood. Can you tell?[/color]

            Bait taken ;-)

            Terry J. Reedy





            Comment

            • Daniel 'Dang' Griffith

              #7
              Re: things I wish python could do

              On Thu, 13 May 2004 12:35:48 -0400, "Terry Reedy" <tjreedy@udel.e du>
              wrote:
              [color=blue]
              >
              >"Jacek Generowicz" <jacek.generowi cz@cern.ch> wrote in message
              >news:tyfk6zgpx 6w.fsf@pcepsft0 01.cern.ch...[color=green]
              >> Ryan Paul <segphault@sbcg lobal.net> writes:[/color]
              >[color=green]
              >> Yes, I find the whole statement expression dichotomy one huge PITA.[/color]
              >[/color]
              .....[color=blue][color=green][color=darkred]
              >> > 4. assignments cant be made inside of anonymous functions.[/color]
              >>
              >> Actually, there's very little you can do in anonymous functions, given
              >> that you are restricted to a _single expression_.[/color]
              >
              >Lambda expressions in Python are strictly an abbreviation for functions
              >with a trivial body ('return expression'), nothing more. Anonymity is a
              >wart, not a feature. The keyword is, to me, a mistake because of the false
              >expectations it stimulates.[/color]

              Maybe lambda should be renamed as lameda? ;-)
              --dang "$.02 too much" dude

              Comment

              • Ville Vainio

                #8
                Re: things I wish python could do

                >>>>> "Terry" == Terry Reedy <tjreedy@udel.e du> writes:

                Terry> Would you really prefer writing <target> = expression as
                Terry> set('<target>', expression) or as the 'special-form'
                Terry> pseudofunction call setq(<target>, expression)? Just about
                Terry> all Python statements are statements because they would
                Terry> also be a PITA, and possibly more so, as expressions.

                Couldn't a = 10 still be an expression despite the syntax? Or at least
                behave like one, i.e. yield a result, probably 10 or None...

                These days I do think it's not that big a deal, though. Putting
                assignment into the condition part of while is a habit people will
                outgrow, and other statements would be very clumsy as expressions.

                --
                Ville Vainio http://tinyurl.com/2prnb

                Comment

                • rzed

                  #9
                  Re: things I wish python could do

                  Daniel 'Dang' Griffith <noemail@noemai l4u.com> wrote in
                  news:e2f2f1d667 57198e867abb285 277d0b5@news.te ranews.com:
                  [color=blue]
                  > On Thu, 13 May 2004 12:35:48 -0400, "Terry Reedy"
                  > <tjreedy@udel.e du> wrote:[/color]
                  [...][color=blue][color=green]
                  >>Lambda expressions in Python are strictly an abbreviation for
                  >>functions with a trivial body ('return expression'), nothing
                  >>more. Anonymity is a wart, not a feature. The keyword is, to
                  >>me, a mistake because of the false expectations it stimulates.[/color]
                  >
                  > Maybe lambda should be renamed as lameda? ;-)[/color]

                  Or: lambada -- the forbidden function.

                  --
                  rzed
                  No more than $0.01 here.

                  Comment

                  • Ville Vainio

                    #10
                    Re: things I wish python could do

                    >>>>> "rzed" == rzed <rzantow@ntelos .net> writes:
                    [color=blue][color=green]
                    >> Maybe lambda should be renamed as lameda? ;-)[/color][/color]

                    rzed> Or: lambada -- the forbidden function.

                    Or leave it to Lambda - the function that dare not speak its name.

                    (I guess I need to get some sleep now)

                    --
                    Ville Vainio http://tinyurl.com/2prnb

                    Comment

                    • Jacek Generowicz

                      #11
                      Re: things I wish python could do

                      "Terry Reedy" <tjreedy@udel.e du> writes:
                      [color=blue]
                      > Would you really prefer writing <target> = expression as set('<target>',
                      > expression) or as the 'special-form' pseudofunction call setq(<target>,
                      > expression)?[/color]

                      Almost. I'd really like to write it thus:

                      (setq <target> expression)

                      then we'd really be getting somewhere.
                      [color=blue][color=green]
                      > > I'm feeling in a controversy-stirring mood. Can you tell?[/color]
                      >
                      > Bait taken ;-)[/color]

                      Here's some more, if you still have the apetite :-)

                      Comment

                      • Michael Walter

                        #12
                        Re: things I wish python could do

                        Ville Vainio wrote:[color=blue]
                        > Couldn't a = 10 still be an expression despite the syntax? Or at least
                        > behave like one, i.e. yield a result, probably 10 or None...[/color]
                        I would think so -- even in C(++) it's possible to write:

                        a = b = 10;

                        Cheers,
                        Michael

                        Comment

                        • David Eppstein

                          #13
                          Re: things I wish python could do

                          In article <2gjoq5F3mvvdU2 @uni-berlin.de>,
                          Michael Walter <cm@leetspeak.o rg> wrote:
                          [color=blue][color=green]
                          > > Couldn't a = 10 still be an expression despite the syntax? Or at least
                          > > behave like one, i.e. yield a result, probably 10 or None...[/color]
                          > I would think so -- even in C(++) it's possible to write:
                          >
                          > a = b = 10;[/color]

                          It's possible to write that in Python, too, but nevertheless in Python
                          the "b = 10" part isn't an expression.

                          --
                          David Eppstein http://www.ics.uci.edu/~eppstein/
                          Univ. of California, Irvine, School of Information & Computer Science

                          Comment

                          Working...