syntactic sugar for def?

Arnaud Delobelle arnodel at gmail.com
Wed Sep 28 17:37:12 EDT 2011


On 28 September 2011 22:26, Ethan Furman <ethan at stoneleaf.us> wrote:
> I remember that 'class' is sugar for type(....).
>
> I don't remember if 'def' is sugar for something besides lambda.
>
> Any clues for me?  Heck, I'll even be grateful for outright answers!

It's not really sugar.  But I think you mean something like this:


>>> class A: pass
...
>>> type(A)
<class 'type'>
>>> type is type(A)
True

So the closest you get for functions will be:

>>> def f(): pass
...
>>> type(f)
<class 'function'>

Try help(type(f)) to see how to use it to create a function object.
The problem is that you need to provide a code object, and the easiest
way to create a code object is to use a def statement :)

HTH

-- 
Arnaud



More information about the Python-list mailing list