Re: syntactic sugar for def?
On Wed, Sep 28, 2011 at 07:01:11PM -0400, Terry Reedy wrote: > On 9/28/2011 5:26 PM, Ethan Furman wrote: > > >I don't remember if 'def' is sugar for something besides lambda. > > That is a bit backwards. > lambda x: expr(x) > is syntactic sugar for > def (x): return expr(x) > del > ;-) > lambda is less sugar and more of just a def as an expression. -- http://mail.python.org/mailman/listinfo/python-list
Re: syntactic sugar for def?
On 9/28/2011 5:26 PM, Ethan Furman wrote: I don't remember if 'def' is sugar for something besides lambda. That is a bit backwards. lambda x: expr(x) is syntactic sugar for def (x): return expr(x) del ;-) -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: syntactic sugar for def?
On Wed, Sep 28, 2011 at 3:26 PM, Ethan Furman wrote: > I remember that 'class' is sugar for type(). > > I don't remember if 'def' is sugar for something besides lambda. This is something I have thought about a lot since PyCon this year. I apologize in advance. Since 3.0, class statements are syntactic sugar for some extra steps beyond "meta(...)" [1]. In CPython this is facilitated through the "hidden" __build_class__() builtin[2]. We have the __import__() builtin for customizing module creation. But, as you asked, what about functions? Currently there isn't a way to customize function creation. There is no __build_function__() builtin. The best you can do is, like others have said, directly call FunctionType(...) or type(f)(...) where f is an existing function. I expect that if there were a __build_function__, it would wrap the code that is currently run for the MAKE_FUNCTION/MAKE_CLOSURE opcodes[3]. Also, both modules and classes have mechanisms built-in to allow for customization in certain parts of the creation process (PEP 302[4] and metaclasses[5], respectively). Functions lack this as well, though there hasn't been a big clamor for it. :) Nick Coghlan proposed an interesting idea for this in March[6], with some later follow-up[7]. Nothing much came of it though. Definitely an interesting topic, which has led me to learn a lot about Python and CPython. -eric [1] http://www.python.org/dev/peps/pep-3115/ http://mail.python.org/pipermail/python-3000/2007-March/006338.html [2] http://hg.python.org/cpython/file/default/Python/bltinmodule.c#l35 [3] http://hg.python.org/cpython/file/default/Python/ceval.c#l2680 [4] http://www.python.org/dev/peps/pep-0302/ http://docs.python.org/release/2.3.5/whatsnew/section-pep302.html http://docs.python.org/dev/reference/simple_stmts.html#the-import-statement [5] http://docs.python.org/dev/reference/datamodel.html#customizing-class-creation http://www.python.org/doc/essays/metaclasses/ [6] http://mail.python.org/pipermail/python-ideas/2011-March/009391.html [7] http://mail.python.org/pipermail/python-ideas/2011-March/009625.html http://mail.python.org/pipermail/python-ideas/2011-April/009765.html > > Any clues for me? Heck, I'll even be grateful for outright answers! > > ~Ethan~ > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: syntactic sugar for def?
En Wed, 28 Sep 2011 18:51:00 -0300, Chris Kaynor escribió: On Wed, Sep 28, 2011 at 2:37 PM, Arnaud Delobelle wrote: On 28 September 2011 22:26, Ethan Furman 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) type is type(A) True So the closest you get for functions will be: def f(): pass ... type(f) 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 :) I would say compile isn't too much harder to use: c = compile('a = 123', 'test', 'exec') d = {} f = types.FunctionType(c, d, 'test') f() print d {'a': 123} Although it appears you get all of the variables defined as global apparently (try "f = types.FunctionType(c, globals(), 'test')" instead). I know no way of compiling a function body alone. Suppose you have this function: def foo(x): print x y = 2*x return y py> compile(" print x\n y = 2*x\n return y", "", "exec") Traceback (most recent call last): File "", line 1, in File "", line 1 print x ^ IndentationError: unexpected indent py> compile("print x\ny = 2*x\nreturn y", "", "exec") Traceback (most recent call last): File "", line 1, in File "", line 3 SyntaxError: 'return' outside function If you include the 'def' statement in the source string, the resulting code object does not represent the function itself, but a "module" defining it: py> f = FunctionType(compile("def foo(x):\n print x\n y = 2*x\n return y\n", ... "", "exec"), globals(), "foo") py> f(3) Traceback (most recent call last): File "", line 1, in TypeError: () takes no arguments (1 given) py> dis.dis(f) 1 0 LOAD_CONST 0 (file "", line 1>) 3 MAKE_FUNCTION0 6 STORE_NAME 0 (foo) 9 LOAD_CONST 1 (None) 12 RETURN_VALUE To get at the actual function code, one should use f.func_code.co_consts[0]; this would be the 'code' parameter for types.FunctionType. Very complicated, really; nothing can beat the 'def' statement for defining a function ;) -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: syntactic sugar for def?
On Wed, Sep 28, 2011 at 2:37 PM, Arnaud Delobelle wrote: > On 28 September 2011 22:26, Ethan Furman 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) > type is type(A) > True > > So the closest you get for functions will be: > def f(): pass > ... type(f) > > > 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 :) I would say compile isn't too much harder to use: >>> c = compile('a = 123', 'test', 'exec') >>> d = {} >>> f = types.FunctionType(c, d, 'test') >>> f() >>> print d {'a': 123} Although it appears you get all of the variables defined as global apparently (try "f = types.FunctionType(c, globals(), 'test')" instead). > > HTH > > -- > Arnaud > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: syntactic sugar for def?
On 28 September 2011 22:26, Ethan Furman 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) >>> type is type(A) True So the closest you get for functions will be: >>> def f(): pass ... >>> type(f) 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: syntactic sugar for def?
On Wed, Sep 28, 2011 at 3:26 PM, Ethan Furman 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! If you mean is there a way to create functions using reflection, you can use types.FunctionType. Like type() requires a dict, FunctionType() requires a code object that must first be compiled. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
syntactic sugar for def?
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! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list