Python 3K or Python 2.9?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Sep 13 05:09:27 CEST 2007


On Wed, 12 Sep 2007 19:40:04 -0700, TheFlyingDutchman wrote:

>>     >>> Foo.bar(foo, "spam")
>>     >>> foo.bar("spam")
> 
> That looks like a case of "There's more than one way to do it". ;) The
> first form is definitely consistent with the method declaration, so
> there's a lot to be said for using that style when teaching people to
> make classes -> send self, receive self.

I think it is a horrible thing to teach beginners. Would you teach them 
to write 1.__add__(1) instead of 1+1?

I think that beginners should be taught to write

instance.method(arguments)

and then only introduced to 

class.method(instance, arguments)

when they progress to needing to know what happens under the hood.



>> The latter two statements are equivalent. The 'instance.method(args)'
>> syntax is just sugar for 'Class.method(instance, args)'.
> 
> I think I saw where Guido Van Rossum had referred to something as
> "syntactic sugar" in some python.org page. I am not familiar with sugar
> as related to syntax. Is it being used as a synonym for "easier way of
> doing it"?

"Syntactic sugar" is not really well defined, it's a fuzzy concept, but 
in a nutshell it is special syntax made to "sweeten" the language by 
giving an easier way to write common tasks.

It's hard to find examples of syntactic sugar that everybody agrees are 
syntactic sugar, but I consider the following to be good (in the sense of 
useful, obvious and well-thought-out) examples:

alist[-1] for alist[len(alist)-1]

list comprehensions and generator expressions


Perhaps less attractive, but still very(?) useful, examples of syntactic 
sugar are decorators and the "X if C else Y" ternary operator.



-- 
Steven



More information about the Python-list mailing list