Ruby like syntactic sugar

David MacQuigg dmq at gain.com
Tue Mar 2 19:25:51 EST 2004


On 2 Mar 2004 13:26:17 -0800, fxj at mpe.mpg.de (F Jamitzky) wrote:

>It is rather easy to define functions in python that mimic the special
>ruby syntactic sugar like:
>
>5.times { print "Hello World!" }
>
>or
>
>[toast, cheese, wine].each { |food| eat food }
>
>In python these fragments can be written as:
>
>5 *times(lambda: printf("Hello World!") )

What is wrong with:
for n in range(5): print("Hello World!")
It doesn't require definition of a new class, and it uses a syntax
everyone past Chapter 1 in Python will find familiar.

There are a few things in Ruby I would like to see in Python.  See the
Working with Strings example at
http://userlinux.com/cgi-bin/wiki.pl?RubyPython but in general, I'm
finding the "syntactic sugar" of Ruby is almost all personal
preference ( -1942.abs vs abs(-1942) ).  I have yet to see an example
of Ruby code blocks that is fundamentally better than the equivalent
in Python.

-- Dave

>or
>
>[toast, cheese, wine] *each (lambda food: eat(food) )
>
>by defining a Times class like that:
>
>class Times:
>    def __rmul__(self,n):
>        for i in range(n):
>            self.func()
>    def __call__(self,func):
>        self.func=func
>        return self
>times=Times()
>
>Was this intended as a language feature or is this style an
>"unpythonic"
>missuse of the syntax ? I like the way of writing loops and list
>comprehensions that way, but I think there will be an outcry of some
>people. What do you guys think about extensions like that ?
>
>cheers
>Ferdinand




More information about the Python-list mailing list