subexpressions

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

    #16
    Re: subexpressions

    Steve Howell wrote:
    >
    The compiler doesn't know the types up front, but if
    you wanted to do this kind of optimization (and you
    believed that 95% of x*x cases would benefit from it,
    and you're willing to sacrifice performance for the 5%
    of folks that overload multiply), then the compiler
    could generate bytecode that set the stage for later
    conditional caching of the first execution of x*x.
    True.
    You'd then need the execution of the bytecodes at
    runtime (ceval.c or something called by it) to work in
    such a way that they only cache the result when side
    effects are not an issue. At runtime you can reliably
    detect whether something is still a virgin builtin,
    correct?
    I've no idea, but I imagine that psyco knows whether or not it has a
    proper built-in number object when it generates specialised code for
    similar cases.
    To my disclaimer, you would only undertake such an
    optimization if multiplication were really, really
    expensive (which I don't think is even true for floats
    today), and even then you'd proceed cautiously.
    Indeed. Some believe that for "full Python" you can only introduce
    such measures at run-time, although extensive enough analysis of the
    code could perhaps suggest suitable specialisations in advance, as
    presumably demonstrated by Shed Skin.

    Paul

    Comment

    • Steven D'Aprano

      #17
      Re: subexpressions

      On Fri, 01 Jun 2007 07:09:50 -0400, Steve Holden wrote:
      >>>>The real answer is of course: Use a function.
      >>>But what about something like
      >>>>
      >>>lambda x: sin(y)+cos(y) where y=x*x
      >>>>
      >>>?
      >>>May be this could be a PEP? If there is no straight way to do this.
      >>def f(x):
      >> y = x*x
      >> return sin(y) + cos(y)
      >>>
      >>What is not straightforward about that?
      >>
      >This code is needed once in a map, so I don't want 3+ extra lines.
      >Solution seemed so simple...
      >I always considered python as languague, where simple things do not require
      >extensive coding.
      >Moreover, this construction is common thing in functional programming.
      >>
      >>
      Stop thinking of three lines as "extensive coding" and your problem
      disappears immediately.

      The F-bot once suggested adding a clause to the Zen of Python about
      "writing two lines of code is not a sin" or "cramming two lines of code
      into one is not a virtue" (my paraphrases).


      Check the two alternatives:

      def f(x):
      y = x*x
      return sin(y) + cos(y)

      44 key presses, including tabs and newlines and a blank line after the
      function, but excluding counting the shift key separately.

      lambda x: (lambda y: sin(y) + cos(y))(x*x)

      42 key presses.

      Apart from the extremely minor issue of "namespace pollution", I think
      that speaks for itself.



      --
      Steven.

      Comment

      • Cousin Stanley

        #18
        Re: subexpressions

        ....
        After years of discussion, Guido has decided
        to leave lambda alone for 3.0.
        >
        It will not be neither expanded, nor removed, nor renamed.
        But it still will be as ugh, ugh, ugh-lee
        as a mule walking backwards ..... ;-)


        --
        Stanley C. Kitching
        Human Being
        Phoenix, Arizona


        ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
        http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
        ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

        Comment

        • Terry Reedy

          #19
          Re: subexpressions


          "Cousin Stanley" <cousinstanley@ hotmail.comwrot e in message
          news:1180756858 _54747@sp12lax. superfeed.net.. .
          |
          | ....
          | After years of discussion, Guido has decided
          | to leave lambda alone for 3.0.
          | >
          | It will not be neither expanded, nor removed, nor renamed.
          |
          | But it still will be as ugh, ugh, ugh-lee
          | as a mule walking backwards ..... ;-)

          Then pretend it was eliminated, as Guido once thought to do, and do not use
          it. And look away when others do ;-)

          tjr




          Comment

          • Stef Mientki

            #20
            Re: subexpressions

            >
            >
            Check the two alternatives:
            >
            def f(x):
            y = x*x
            return sin(y) + cos(y)
            >
            44 key presses, including tabs and newlines and a blank line after the
            function, but excluding counting the shift key separately.
            >
            lambda x: (lambda y: sin(y) + cos(y))(x*x)
            >
            42 key presses.
            >
            Apart from the extremely minor issue of "namespace pollution", I think
            that speaks for itself.
            and now I've only 60 lines on my screen,
            so what about

            def f(x): y = x*x; return sin(y)+cos(y);

            cheers,
            Stef Mientki

            Comment

            • Alex Martelli

              #21
              Re: subexpressions (OT: math)

              Stebanoid@gmail .com <Stebanoid@gmai l.comwrote:
              On 3 , 22:07, "Steban...@gmai l.com" <Steban...@gmai l.comwrote:

              angle is a ratio of two length and
              dimensionless.http://en.wikipedia.org/wiki/Angle#U...easure_for_ang
              les

              only dimensionless values can be a argument of a sine and exponent!
              Are you discordant?
              >
              if you are discordant read more :P :
              sine is a dimensionless value.
              if we expand sine in taylor series sin(x) = x - (x^3)/6 + (x^5)/120
              etc.
              you can see that sin can be dimensionless only if x is dimensionless
              too.
              >
              I am a professional physicist and a know about what I talk
              Lots of people are confused by the concept of "degrees" -- your Taylor
              series, of course, intrinsically assumes x is "in radians" (which of
              course IS how angles "truly are"). I blame the Babylonians for that
              confusion just as much as for the clunky base-60 that intrudes in our
              ordinary time reckoning...!


              Alex

              Comment

              Working...