473,396 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Ruby like syntactic sugar

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
Jul 18 '05 #1
5 1754
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
--
Riccardo Galli
Sideralis Programs
http://www.sideralis.net
Jul 18 '05 #2
On 2 Mar 2004 13:26:17 -0800,
F Jamitzky <fx*@mpe.mpg.de> wrote:
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
Jul 18 '05 #3
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.
Jul 18 '05 #4
On 2 Mar 2004 13:26:17 -0800, fx*@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


Jul 18 '05 #5

"F Jamitzky" <fx*@mpe.mpg.de> wrote in message
news:53*************************@posting.google.co m...
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


Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
0
by: Christopher T King | last post by:
Okay, so this is really two requests in one, and they're both kinda outlandish, but I'm gonna post them nonetheless: I've always thought xrange() to be ugly; it looks to be a lot of typing just...
13
by: Wayne Folta | last post by:
I've been a long-time Perl programmer, though I've not used a boatload of packages nor much of the tacky OO. A couple of years ago, I decided to look into Python and Ruby. Python looked OK, but...
12
by: Bob Stearns | last post by:
This is probably the wrong forum for this, but I thought it might start some discussion. The INSERT statement, in its current form, has problems being formatted so a human reader can follow it....
13
by: Neil Zanella | last post by:
Hello, It seems to me that C# properties are nothing more than syntactic sugar for getters and setters. I wonder whether others hold a different point of view. Basically, what more do they have...
4
by: Bas | last post by:
Hi group, just out of curiosity, is there a list of all the syntactic sugar that is used in python? If there isn't such a list, could it be put on a wiki somewhere? The bit of sugar that I do...
34
by: glomde | last post by:
i I would like to extend python so that you could create hiercical tree structures (XML, HTML etc) easier and that resulting code would be more readable than how you write today with packages like...
13
by: Sam Kong | last post by:
Hi, While discussing C#'s using statement, a guy and I had an argument. In C# spec (15.13), there's an explanation like the following. using (R r1 = new R()) { r1.F(); } is precisely...
11
by: Helmut Jarausch | last post by:
Hi, are decorators more than just syntactic sugar in python 2.x and what about python 3k ? How can I find out the predefined decorators? Many thanks for your help, Helmut Jarausch
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.