Ruby like syntactic sugar

F

F Jamitzky

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!") )

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
 
R

Riccardo Galli

On Tue, 02 Mar 2004 13:26:17 -0800, F Jamitzky wrote:

[snip]
In python these fragments can be written as:

5 *times(lambda: printf("Hello World!") )
[...]

printf ? a bit c-style, isn't it?
may be did you want to write sys.stdout.write instead?

Ciao,
Riccardo
 
A

A.M. Kuchling

5 *times(lambda: printf("Hello World!") )
or
[toast, cheese, wine] *each (lambda food: eat(food) )

These formulations are no improvement over the obvious code:

for i in range(5):
print 'Hello world!'

or

for food in [toast, cheese, wine]:
eat(food)

It's certainly possible to use Python language features to get Ruby-like
code, but this code isn't as clear, so why bother?

--amk
 
W

Wayne Folta

It seems your solution could be the basis for rubyesque Python. (Do you
have it backwards regarding Ruby, though? Isn't 5.times the way a loop
is done and the for-loop-looking construct is the syntactic sugar?)

But on a personal note, I'd add that the
5.times { print "Hello World!" }

syntax is the idiom that persuaded me Ruby is not for me. In my
opinion, this construct is either too clever by half or it's too
Smalltalkish (everything must be classed). It makes some sense when
spoken, but I fail to see how a looping method belongs to the class of
numbers, so it troubles me too much to use or to enjoy a language where
this is natural.
 
D

David MacQuigg

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
 
T

Terry Reedy

F Jamitzky said:
Was this intended as a language feature or is this style an
"unpythonic"

It is intentional that Python give you the flexibility to do things the
developers never imagined and that most users think a bit wierd or 'over
the top'. Playing with things like this is one way to learn, and probably
more fun than some.

Terry J. Reedy
 
R

Raymond Hettinger

[F Jamitzky]
[Terry Reedy]
It is intentional that Python give you the flexibility to do things the
developers never imagined and that most users think a bit wierd or 'over
the top'. Playing with things like this is one way to learn, and probably
more fun than some.

QOTW: Gets my vote for quote of the week.


Raymond Hettinger
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top