define loop statement?

Discussion in 'Python' started by David Isaac, Feb 16, 2006.

  1. David Isaac

    David Isaac Guest

    I would like to be able to define a loop statement
    (nevermind why) so that I can write something like

    loop 10:
    do_something

    instead of

    for i in range(10):
    do_something

    Possible? If so, how?

    Thanks,
    Alan Isaac
    David Isaac, Feb 16, 2006
    #1
    1. Advertising

  2. No, not in the way you think it is. What you can do instead is
    something like this:

    def do_something(i):
    ... do_something ...

    def loop(n, func):
    for i in range(n): func(i)

    loop(10, do_something)
    Jonathan Gardner, Feb 16, 2006
    #2
    1. Advertising

  3. David Isaac

    Rene Pijlman Guest

    David Isaac:
    >I would like to be able to define a loop statement
    >(nevermind why) so that I can write something like
    >
    >loop 10:
    > do_something
    >
    >instead of
    >
    >for i in range(10):
    > do_something
    >
    >Possible? If so, how?


    Yes. By implementing a compiler or an interpreter for your programming
    language. Or a preprocessor that converts your language to Python, or some
    other suitable intermediate language. Or a programmer, that converts your
    pseudocode and some coffee to the desired algorithm :)

    --
    René Pijlman
    Rene Pijlman, Feb 16, 2006
    #3
  4. David Isaac

    Georg Brandl Guest

    David Isaac wrote:
    > I would like to be able to define a loop statement
    > (nevermind why) so that I can write something like
    >
    > loop 10:
    > do_something
    >
    > instead of
    >
    > for i in range(10):
    > do_something
    >
    > Possible? If so, how?


    It's not possible to create a new statement, with suite
    and indentation rules without hacking the interpreter or
    resorting to alternative bytecode compilers such as "pyc".

    Creating a _function_ named "loop" is easy as Jonathan's
    answer shows.

    Georg
    Georg Brandl, Feb 16, 2006
    #4
  5. David Isaac

    Guest

    David Isaac:
    > I would like to be able to define a loop statement
    > (nevermind why) so that I can write something like
    >
    > loop 10:
    > do_something
    >
    > instead of
    >
    > for i in range(10):
    > do_something
    >
    > Possible? If so, how?


    It seems that you are looking for macros; maybe Logix "language"
    (www.livelogix.net/logix) or Boo (boo.codehaus.org) can solve your
    problem.
    Otherwise you can do it with very different languages like Lisp or
    Scheme (with a different syntax) and maybe Dylan too.

    If you tell us your purpose, maybe we can suggest you a
    better/different solution.

    Bye,
    bearophile
    , Feb 17, 2006
    #5
  6. David Isaac

    Xavier Morel Guest

    Rene Pijlman wrote:
    > David Isaac:
    >> I would like to be able to define a loop statement
    >> (nevermind why) so that I can write something like
    >>
    >> loop 10:
    >> do_something
    >>
    >> instead of
    >>
    >> for i in range(10):
    >> do_something
    >>
    >> Possible? If so, how?

    >
    > Yes. By implementing a compiler or an interpreter for your programming
    > language. Or a preprocessor that converts your language to Python, or some
    > other suitable intermediate language. Or a programmer, that converts your
    > pseudocode and some coffee to the desired algorithm :)
    >

    Or by hacking through the Python source and creating his own "somehow
    pythonish but absolutely not python" language
    Xavier Morel, Feb 17, 2006
    #6
  7. David Isaac

    Xavier Morel Guest

    Rene Pijlman wrote:
    > David Isaac:
    >> I would like to be able to define a loop statement
    >> (nevermind why) so that I can write something like
    >>
    >> loop 10:
    >> do_something
    >>
    >> instead of
    >>
    >> for i in range(10):
    >> do_something
    >>
    >> Possible? If so, how?

    >
    > Yes. By implementing a compiler or an interpreter for your programming
    > language. Or a preprocessor that converts your language to Python, or some
    > other suitable intermediate language. Or a programmer, that converts your
    > pseudocode and some coffee to the desired algorithm :)
    >

    Or by hacking through the Python source and creating his own "somehow
    pythonish but absolutely not python" language
    Xavier Morel, Feb 17, 2006
    #7
  8. David Isaac wrote:
    > I would like to be able to define a loop statement
    > (nevermind why) so that I can write something like
    >
    > loop 10:
    > do_something
    >
    > instead of
    >
    > for i in range(10):
    > do_something
    >
    > Possible? If so, how?


    Ruby and Smalltalk are both good at this kind of thing, since they have
    syntactic support for associating a block with each method call. In
    Python, I think you just have to do a little more setup. How about
    something like this?

    class Loop:
    def __init__(self, n):
    self.n = n
    def __call__(self):
    self.n = self.n - 1
    return self.n != 0


    if __name__ == '__main__':
    loop = Loop(10)
    while loop:
    print "OK"
    Jeffrey Schwab, Feb 18, 2006
    #8
  9. David Isaac

    Georg Brandl Guest

    Jeffrey Schwab wrote:

    > class Loop:
    > def __init__(self, n):
    > self.n = n
    > def __call__(self):
    > self.n = self.n - 1
    > return self.n != 0
    >
    >
    > if __name__ == '__main__':
    > loop = Loop(10)
    > while loop:
    > print "OK"


    Seems you forgot "()" after "while loop" above.

    Georg
    Georg Brandl, Feb 18, 2006
    #9
  10. Jeffrey Schwab wrote:

    > class Loop:
    > def __init__(self, n):
    > self.n = n
    > def __call__(self):
    > self.n = self.n - 1
    > return self.n != 0
    >
    >
    > if __name__ == '__main__':
    > loop = Loop(10)
    > while loop:


    Whoops. Should be "while loop()".

    > print "OK"
    Jeffrey Schwab, Feb 18, 2006
    #10
  11. David Isaac

    David Isaac Guest


    > Alan Isaac wrote:
    > > I would like to be able to define a loop statement
    > > (nevermind why) so that I can write something like
    > >
    > > loop 10:
    > > do_something
    > >
    > > instead of
    > >
    > > for i in range(10):
    > > do_something
    > >
    > > Possible? If so, how?



    "Jeffrey Schwab" <> wrote in message
    news:T4LJf.14256$...
    > class Loop:
    > def __init__(self, n):
    > self.n = n
    > def __call__(self):
    > self.n = self.n - 1
    > return self.n != 0
    >
    >
    > if __name__ == '__main__':
    > loop = Loop(10)
    > while loop():
    > print "OK"


    OK, that's pretty good.
    Thanks!
    Alan Isaac
    David Isaac, Feb 18, 2006
    #11
  12. Em Sáb, 2006-02-18 às 20:04 +0000, Jeffrey Schwab escreveu:
    > if __name__ == '__main__':
    > loop = Loop(10)
    > while loop:
    > print "OK"


    Maybe:

    while Loop(10)():
    print "OK"

    Looks rather ugly but requires one less line ;-).

    --
    "Quem excele em empregar a força militar subjulga os exércitos dos
    outros povos sem travar batalha, toma cidades fortificadas dos outros
    povos sem as atacar e destrói os estados dos outros povos sem lutas
    prolongadas. Deve lutar sob o Céu com o propósito primordial da
    'preservação'. Desse modo suas armas não se embotarão, e os ganhos
    poderão ser preservados. Essa é a estratégia para planejar ofensivas."

    -- Sun Tzu, em "A arte da guerra"
    Felipe Almeida Lessa, Feb 18, 2006
    #12
  13. David Isaac

    Nigel Rowe Guest

    Felipe Almeida Lessa wrote:

    > Em Sáb, 2006-02-18 às 20:04 +0000, Jeffrey Schwab escreveu:
    >> if __name__ == '__main__':
    >> loop = Loop(10)
    >> while loop:
    >> print "OK"

    >
    > Maybe:
    >
    > while Loop(10)():
    > print "OK"
    >
    > Looks rather ugly but requires one less line ;-).
    >

    Doesn't work. You get a NEW Loop(10) instance on each pass through the
    'while'. This is just an expensive way to make an endless loop.

    --
    Nigel Rowe
    A pox upon the spammers that make me write my address like..
    rho (snail) swiftdsl (stop) com (stop) au
    Nigel Rowe, Feb 19, 2006
    #13
  14. Em Dom, 2006-02-19 às 11:08 +1100, Nigel Rowe escreveu:
    > Felipe Almeida Lessa wrote:
    >
    > > Em Sáb, 2006-02-18 às 20:04 +0000, Jeffrey Schwab escreveu:
    > >> if __name__ == '__main__':
    > >> loop = Loop(10)
    > >> while loop:
    > >> print "OK"

    > >
    > > Maybe:
    > >
    > > while Loop(10)():
    > > print "OK"
    > >
    > > Looks rather ugly but requires one less line ;-).
    > >

    > Doesn't work. You get a NEW Loop(10) instance on each pass through the
    > 'while'. This is just an expensive way to make an endless loop.


    Oh, sorry, ignore me on that one. Now I think I should sleep =S...

    --
    "Quem excele em empregar a força militar subjulga os exércitos dos
    outros povos sem travar batalha, toma cidades fortificadas dos outros
    povos sem as atacar e destrói os estados dos outros povos sem lutas
    prolongadas. Deve lutar sob o Céu com o propósito primordial da
    'preservação'. Desse modo suas armas não se embotarão, e os ganhos
    poderão ser preservados. Essa é a estratégia para planejar ofensivas."

    -- Sun Tzu, em "A arte da guerra"
    Felipe Almeida Lessa, Feb 19, 2006
    #14
  15. David Isaac

    Benji York Guest

    David Isaac wrote:
    > I would like to be able to define a loop statement
    > (nevermind why) so that I can write something like
    >
    > loop 10:
    > do_something


    Here's a flagrant hack:

    import sys

    VAR_NAME = '__repeat_counter'

    def set_repeat_counter(value):
    frame = sys._getframe(2)
    frame.f_locals[VAR_NAME] = value

    def get_repeat_counter(value):
    frame = sys._getframe(2)
    if VAR_NAME not in frame.f_locals:
    frame.f_locals[VAR_NAME] = value

    return frame.f_locals[VAR_NAME]

    def repeat(limit):
    set_repeat_counter(get_repeat_counter(limit)-1)
    return get_repeat_counter(limit)

    while repeat(10):
    print 'OK'

    Without more work it doesn't allow nested loops though.

    And for the record, if you're worrying about Python's counted loop
    construct you need better things to worry about.
    <insert-smilies-as-appropriate>
    --
    Benji York
    Benji York, Feb 19, 2006
    #15
  16. David Isaac

    David Isaac Guest

    "Benji York" <> wrote in message
    news:...
    > Here's a flagrant hack:


    Admiration wins out over revulsion. ;-)
    Thanks,
    Alan Isaac

    PS Here's the motivation. Python closely resembles pseudocode. With
    a very little LaTeX hacking, it is often possible to write algorithms is
    Python that typeset as reasonable pseudocode. A simple repetitive
    loop is a bit of a sticking point.
    David Isaac, Feb 19, 2006
    #16
  17. In article <%50Kf.1181$p13.791@trnddc08>,
    David Isaac <> wrote:
    .
    .
    .
    >Admiration wins out over revulsion. ;-)
    >Thanks,
    >Alan Isaac
    >
    >PS Here's the motivation. Python closely resembles pseudocode. With
    >a very little LaTeX hacking, it is often possible to write algorithms is
    >Python that typeset as reasonable pseudocode. A simple repetitive
    >loop is a bit of a sticking point.
    >
    >


    Wow. Innovative.
    Cameron Laird, Feb 19, 2006
    #17
  18. David Isaac

    Magnus Lycka Guest

    David Isaac wrote:

    > PS Here's the motivation. Python closely resembles pseudocode. With
    > a very little LaTeX hacking, it is often possible to write algorithms is
    > Python that typeset as reasonable pseudocode. A simple repetitive
    > loop is a bit of a sticking point.


    With slightly more LaTeX hacking or possibly Python hacking on your
    script before passing it to LaTeX, you might just be able to change
    occurences of

    for x in range(a, b):

    to

    for x in [a, b):

    or whatever, and get away with plain vanilla Python
    in your code...
    Magnus Lycka, Feb 22, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NKOBAYE027

    weird #define statement

    NKOBAYE027, Mar 2, 2004, in forum: C++
    Replies:
    8
    Views:
    1,704
    David White
    Mar 2, 2004
  2. theotyflos
    Replies:
    3
    Views:
    466
    Thomas Matthews
    Feb 19, 2004
  3. robin liu
    Replies:
    3
    Views:
    821
    Robin Liu
    Apr 21, 2006
  4. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    459
    Brian Takita
    Jan 23, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    370
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page