473,505 Members | 14,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

List of all syntactic sugar?

Bas
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 know have helped me a lot in
understanding the inner workings of python.

To give a few examples (might not be totally correct):

x[i] -> x.__getitem__(i)
x[a:b] -> x.__getitem__(slice(a,b,None))
x+y -> x._add__(y)
x.method(a) -> call (x.__dict__[method], self, a) ??
for i in x: f(i) -> it = iter(x); while True: i = it.next(); f(i) ....
except stop: pass

TIA,
Bas

Apr 14 '06 #1
4 8881
Bas:
just out of curiosity, is there a list of all the syntactic sugar that
is used in python?


http://docs.python.org/ref/specialnames.html

--
René Pijlman
Apr 14 '06 #2
On Fri, 14 Apr 2006 02:54:11 -0700, Bas wrote:
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 know have helped me a lot in
understanding the inner workings of python.


That kind of depends on what you mean by syntactic sugar. For instance, I
wouldn't call any of your examples syntactic sugar. The problem with
(e.g.) calling x[i] syntactic sugar for x.__getitem__(i) is that it gets
the relationship backwards: x[i] _is_ the syntax, it isn't the sugar.
Magic methods like __getitem__ are there as a mechanism to allow custom
classes to use the same syntax as built-in classes.

However, I would call these syntactic sugar:

"hello" " " "world" -> "hello world" # literal string concatenation

r'raw strings'

x[-n] -> x[len(x)-n]

x[:] -> x[0:len(x)]

print obj -> sys.stdout.write(str(obj) + '\n')

print obj, -> sys.stdout.write(str(obj))

s.find(target) ->
try:
return s.index(target)
except IndexError:
return -1
# not really syntax, perhaps "method sugar" is a better name?

import a, b, c -> import a; import b; import c

raise Exception, string -> raise Exception(string)
Of course, one person's syntactic sugar is another person's essential
syntactic carbohydrates.

--
Steven.

Apr 14 '06 #3
Bas
>That kind of depends on what you mean by syntactic sugar.

Mightbe I was misusing the name of syntactic sugar, but I what I
intended to say was "all the possible 'transformations' that can be
made to reduce all the 'advanced' syntax to some sort of minimal core
of the language".

Bas

Apr 14 '06 #4
> That kind of depends on what you mean by syntactic sugar. For instance, I
wouldn't call any of your examples syntactic sugar.


AFAIK that is exactly what syntactic sugar means. Apart from non-strictness,
all syntax can be expressed by function application. Even

foo.bar()

is nothing but

bar(self)

So - he used the term right I'd say.

Regards,

Diez
Apr 14 '06 #5

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

Similar topics

5
1756
by: F Jamitzky | last post by:
It is rather easy to define functions in python that mimic the special ruby syntactic sugar like: 5.times { print "Hello World!" } or .each { |food| eat food } In python these fragments...
12
1591
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
5648
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...
34
1800
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...
6
2154
by: Heiko Wundram | last post by:
Hi all! The following PEP tries to make the case for a slight unification of for statement and list comprehension syntax. Comments appreciated, including on the sample implementation. ===...
13
2495
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...
35
29181
by: erikwickstrom | last post by:
Hi all, I'm sorry about the newbie question, but I've been searching all afternoon and can't find the answer! I'm trying to get this bit of code to work without triggering the IndexError. ...
7
19451
by: Ivan Voras | last post by:
For a declaration like: List<MyTypeitems = ... where MyType is a struct, attempt to modify an item with items.member = something; fails with the message:
11
2079
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
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7471
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
5613
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,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.