python3: 'where' keyword

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

    #16
    Re: python3: 'where' keyword

    AdSR <adsr@poczta.on et.pl> writes:[color=blue][color=green]
    > > Killer app for this keyword:
    > > class C(object):
    > > x = property(get, set) where:
    > > def get(self):
    > > return "Silly property"
    > > def set(self, val):
    > > self.x = "Told you it was silly"[/color]
    >
    > Hey, this is super-elegant![/color]

    Heh, even further:

    z = C() where:
    class C(object):
    ...

    Lets you make anonymous classes and singleton objects.

    Comment

    • Bengt Richter

      #17
      Re: python3: 'where' keyword

      On Sat, 08 Jan 2005 16:42:16 +1000, Nick Coghlan <ncoghlan@iinet .net.au> wrote:
      [color=blue]
      >Nick Coghlan wrote:[color=green]
      >> It also allows the necessary but uninteresting setup for an expression
      >> to be moved "out of the way", bringing the expression that does the real
      >> work to prominence.[/color]
      >
      >Killer app for this keyword:
      >
      >class C(object):
      >
      > x = property(get, set) where:
      > def get(self):
      > return "Silly property"
      > def set(self, val):
      > self.x = "Told you it was silly"
      >[/color]

      Yes, that is cool and it _is_ an interesting idea. Are suites nestable? E.g., is this legal?

      x = term1 + term2 where:
      term1 = a*b where:
      a = 123
      b = 456
      term2 = math.pi

      Reminds me of some kind of weird let ;-)

      And, is the whole thing after the '=' an expression? E.g.,

      x = ( foo(x) where:
      x = math.pi/4.0
      ) where:
      def foo(x): print 'just for illustration', x

      or is this legal?

      for y in ([foo(x) for x in bar] where:
      bar = xrange(5)
      ): baz(y) where:
      def baz(arg): return arg*2

      Not trying to sabotage the idea, really, just looking for clarification ;-)

      Regards,
      Bengt Richter

      Comment

      • Andrey Tatarinov

        #18
        Re: python3: 'where' keyword

        Bengt Richter wrote:[color=blue][color=green][color=darkred]
        >>>It also allows the necessary but uninteresting setup for an expression
        >>>to be moved "out of the way", bringing the expression that does the real
        >>>work to prominence.[/color]
        >>Killer app for this keyword:
        >>
        >>class C(object):
        >>
        >> x = property(get, set) where:
        >> def get(self):
        >> return "Silly property"
        >> def set(self, val):
        >> self.x = "Told you it was silly"[/color]
        > Yes, that is cool and it _is_ an interesting idea. Are suites nestable? E.g., is this legal?[/color]
        ....[color=blue]
        > And, is the whole thing after the '=' an expression? E.g.,
        >
        > x = ( foo(x) where:
        > x = math.pi/4.0
        > ) where:
        > def foo(x): print 'just for illustration', x
        >
        > or is this legal?
        >
        > for y in ([foo(x) for x in bar] where:
        > bar = xrange(5)
        > ): baz(y) where:
        > def baz(arg): return arg*2
        >
        > Not trying to sabotage the idea, really, just looking for clarification ;-)[/color]

        yes, all your examples are correct. And that's the way I'd like to use
        this feature.

        Comment

        • Andrey Tatarinov

          #19
          Re: python3: 'where' keyword

          Nick Coghlan wrote:[color=blue][color=green]
          >> It also allows the necessary but uninteresting setup for an expression
          >> to be moved "out of the way", bringing the expression that does the
          >> real work to prominence.[/color]
          > Killer app for this keyword:
          >
          > class C(object):
          >
          > x = property(get, set) where:
          > def get(self):
          > return "Silly property"
          > def set(self, val):
          > self.x = "Told you it was silly"[/color]

          oh, that's great! I can't imagine prettier example

          Comment

          • André

            #20
            Re: python3: 'where' keyword

            At the risk of generating controversy, here's another type of example:

            def gcd(a, b):
            where:
            a: int, b: int
            return c where:
            c: int
            while a:
            a, b = b%a, a
            return b

            More can be found at http://aroberge.blogspot.com

            Andre

            Comment

            • André

              #21
              Re: python3: 'where' keyword

              Darn space-eater google groups :-( Here is it again, at teh risk of
              generating controversy

              ..def gcd(a, b):
              .. where:
              .. a: int, b: int
              .. return c where:
              .. c: int
              .. while a:
              .. a, b = b%a, a
              .. return b
              more examples can be found at aroberge.blogsp ot.com

              André

              Comment

              • oren@REMOVETHIS1.hishome.net

                #22
                Re: python3: 'where' keyword

                When I first saw this I thought: "hmmm... this seems as redundant as
                adding a repeat/until loop to Python; there's no chance in hell it will
                ever be accepted by the community or Guido, but I actually kinda like
                it". It's nice to see mostly positive reactions to this idea so far.

                I think it's a really ingenious solution to the the anonymous function
                problem - don't make it anonymous! A short, throwaway name with a very
                localized scope is as good as a truly anonymous function and feels more
                Pythonic to me. We thought we wanted a better syntax than lambda for
                anonymous functions but Andrey shows that perhaps it wasn't what we
                really need. What we need is a solution to quickly and cleanly generate
                bits of callable code without polluting the containing namespace,
                without having to think too hard about unique names and while making
                their temporary and local nature clear from the context. Anonymity
                isn't one of the requirements.

                I really liked Nick Coghlan's property example. The names 'get' and
                'set' are too short and generic to be used without a proper scope but
                with this syntax they are just perfect.

                Here's another example:

                w = Widget(color=Re d, onClick=onClick , onMouseOver=onM ouseOver) where:
                .. def onClick(event): do_this(event.x , event.y, foo)
                .. def onMouseOver(eve nt): someotherwidget .do_that()

                The "onClick=onClic k" part seems a bit redundant, right? So how about
                this:

                w = Widget(**kw) where:
                .. color = Red
                .. def onClick(event): do_this(event.x , event.y, blabla)
                .. def onMouseOver(eve nt): someotherwidget .do_that()
                .. x, y = 100, 200
                .. kw = locals()

                I'm not really sure myself how much I like this. It has a certain charm
                but also feels like abuse of the feature. Note that "w =
                Widget(**locals ()) where:" would produce the wrong result as it will
                include all the values in the containing scope, not just those defined
                in the where block.

                Oren

                Comment

                • Peter Hansen

                  #23
                  Re: python3: 'where' keyword

                  Andrey Tatarinov wrote:[color=blue][color=green][color=darkred]
                  > >>> print words[3], words[5] where:
                  > >>> words = input.split()[/color][/color]
                  >
                  > - defining variables in "where" block would restrict their visibility to
                  > one expression[/color]

                  Then your example above doesn't work... print takes a
                  sequence of expressions, not a tuple as you seem to think.

                  -Peter

                  Comment

                  • Carlos Ribeiro

                    #24
                    Re: python3: 'where' keyword

                    On Sat, 08 Jan 2005 12:53:05 -0500, Peter Hansen <peter@engcorp. com> wrote:[color=blue]
                    > Andrey Tatarinov wrote:[color=green][color=darkred]
                    > > >>> print words[3], words[5] where:
                    > > >>> words = input.split()[/color]
                    > >
                    > > - defining variables in "where" block would restrict their visibility to
                    > > one expression[/color]
                    >
                    > Then your example above doesn't work... print takes a
                    > sequence of expressions, not a tuple as you seem to think.[/color]

                    I found it strange that he had chosen to make the example with
                    "print", that is a statement. I'm not sure how could it be made to
                    work with both expressions and statements, it just seems strange...

                    Overall, I found the idea interesting. It seems like a obvious
                    counterpart to "with", in the sense that both act as modifiers to the
                    scoping rules. I think it can be made to work, and that it would lead
                    to elegant & readable code, but there are still lots of things to
                    consider: exception handling, fast name lookup in the "where" block,
                    access to symbols outside the "where" block, just to name a few.

                    --
                    Carlos Ribeiro
                    Consultoria em Projetos
                    blog: http://rascunhosrotos.blogspot.com
                    blog: http://pythonnotes.blogspot.com
                    mail: carribeiro@gmai l.com
                    mail: carribeiro@yaho o.com

                    Comment

                    • Carl Banks

                      #25
                      Re: python3: 'where' keyword

                      Peter Hansen wrote:[color=blue]
                      > Andrey Tatarinov wrote:[color=green][color=darkred]
                      > > >>> print words[3], words[5] where:
                      > > >>> words = input.split()[/color]
                      > >
                      > > - defining variables in "where" block would restrict their[/color][/color]
                      visibility to[color=blue][color=green]
                      > > one expression[/color]
                      >
                      > Then your example above doesn't work... print takes a
                      > sequence of expressions, not a tuple as you seem to think.[/color]

                      You misunderstand. There "where" is not part of the expression but the
                      statement. The above example would be a modified print statement, a
                      print...where statement, if you will. Under this suggestion, there
                      would be modified versions of various simple statements.

                      This wouldn't be a problem parsing, of course, because "where" would be
                      a keyword.


                      --
                      CARL BANKS

                      Comment

                      • Paul Rubin

                        #26
                        Re: python3: 'where' keyword

                        "Carl Banks" <invalidemail@a erojockey.com> writes:[color=blue]
                        > You misunderstand. There "where" is not part of the expression but the
                        > statement. The above example would be a modified print statement, a
                        > print...where statement, if you will. Under this suggestion, there
                        > would be modified versions of various simple statements.[/color]

                        You mean I can't say

                        # compute sqrt(2) + sqrt(3)
                        x = (sqrt(a) where:
                        a = 2.) \
                        + sqrt (a) where:
                        a = 3.

                        Hmmm.

                        Comment

                        • Carl Banks

                          #27
                          Re: python3: 'where' keyword


                          Bengt Richter wrote:[color=blue]
                          > And, is the whole thing after the '=' an expression? E.g.,
                          >
                          > x = ( foo(x) where:
                          > x = math.pi/4.0
                          > ) where:
                          > def foo(x): print 'just for illustration', x[/color]

                          How would that be any improvement over this?

                          .. x = foo(x) where:
                          .. x = math.pi/4.0
                          .. def foo(x): print 'just for illustration', x

                          Can anyone think of a use case for embedding "where" inside an
                          expression as opposed to making it part of a simple statement? And, if
                          so, is the benefit of it worth the massive hit in readability.

                          [color=blue]
                          > or is this legal?
                          >
                          > for y in ([foo(x) for x in bar] where:
                          > bar = xrange(5)
                          > ): baz(y) where:
                          > def baz(arg): return arg*2[/color]

                          Here, I can only hope not. One reason I proposed a where...do syntax
                          is so that, if you wanted to localize a variable to a for loop or some
                          other compound statement, you could do it with a minimum of fuss.

                          .. where:
                          .. bar = xrange(5)
                          .. def baz(arg): return arg*2
                          .. do:
                          .. for y in [foo(x) for x in bar]:
                          .. baz(y)

                          [color=blue]
                          > Not trying to sabotage the idea, really, just looking for[/color]
                          clarification ;-)

                          That's ok. For it fly, it's got to be able to withstand the flak.
                          --
                          CARL BANKS

                          Comment

                          • Carl Banks

                            #28
                            Re: python3: 'where' keyword


                            Paul Rubin wrote:[color=blue]
                            > "Carl Banks" <invalidemail@a erojockey.com> writes:[color=green]
                            > > You misunderstand.[/color][/color]

                            BTW, Peter, I guess I should have said "I misunderstand, but it can be
                            legal if you consider it part of the statements", since it appears the
                            author did intend it to be part of an expression.

                            [color=blue][color=green]
                            > > There "where" is not part of the expression but the
                            > > statement. The above example would be a modified print statement,[/color][/color]
                            a[color=blue][color=green]
                            > > print...where statement, if you will. Under this suggestion, there
                            > > would be modified versions of various simple statements.[/color]
                            >
                            > You mean I can't say
                            >
                            > # compute sqrt(2) + sqrt(3)
                            > x = (sqrt(a) where:
                            > a = 2.) \
                            > + sqrt (a) where:
                            > a = 3.
                            >
                            > Hmmm.[/color]

                            What would be the advantage of that over this?

                            .. x = sqrt(a) + sqrt(b) where:
                            .. a = 2.0
                            .. b = 3.0

                            Where would making "where" part of an expression rather than part of
                            the statement help? Can you think of a place? ("That it makes Python
                            more like LISP" is not a good enough answer for me, BTW. But feel free
                            to try. :)


                            --
                            CARL BANKS

                            Comment

                            • Paul Rubin

                              #29
                              Re: python3: 'where' keyword

                              "Carl Banks" <invalidemail@a erojockey.com> writes:[color=blue][color=green]
                              > > # compute sqrt(2) + sqrt(3)
                              > > x = (sqrt(a) where:
                              > > a = 2.) \
                              > > + sqrt (a) where:
                              > > a = 3.
                              > >
                              > > Hmmm.[/color]
                              >
                              > What would be the advantage of that over this?
                              >
                              > . x = sqrt(a) + sqrt(b) where:
                              > . a = 2.0
                              > . b = 3.0[/color]

                              The idea of "where" is to allow re-using variable names instead of
                              having to keep track of which ones are in use. I just tried to give a
                              very simple example of how you might do that more than once in a
                              statement.

                              Comment

                              • Carl Banks

                                #30
                                Re: python3: accessing the result of 'if'

                                Nick Coghlan wrote:[color=blue]
                                > I have a different suggestion for this.
                                >
                                > 'as' is used for renaming in import statements. 'as' will be used for[/color]
                                exception[color=blue]
                                > naming in Python 3k.
                                >
                                > So let's use it for expression naming in 'if' statements, too.
                                >
                                > if someregexp.matc h(s) as m:
                                > # blah using m
                                > elif someotherregexp .match(s) as m:
                                > # blah using m[/color]


                                What if the condition you wanted to test wasn't the same as the thing
                                you want to save? In other words, how would you convert this?

                                .. where:
                                .. m = something()
                                .. if m > 20:
                                .. do_something_wi th(m)

                                What you propose works for typical regexps idiom but not for the
                                slightly more general case. However, I could see why some people might
                                not like the where...if syntax I proposed; it's kind of choppy and not
                                exactly easy to follow at a first glance.

                                As a compromise, howabout:

                                .. if m > 20 where m=something():
                                .. do_something_wi th(m)

                                In this case, the m=something() is NOT an assignment statement, but
                                merely a syntax resembling it. The "where m=something()" is part of
                                the if-statement, not the if-expression. It causes m to be visisble in
                                the if-expression and the if-block.

                                It (or your suggestion) could work with a while-loop too.

                                .. while line where line=f.readline ():
                                .. do_something_wi th(line)


                                The main problem here (as some would see it) is that you can't do
                                something this:

                                .. if m > 20 where (def m(): a(); b()):


                                --
                                CARL BANKS

                                Comment

                                Working...