469,622 Members | 2,060 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,622 developers. It's quick & easy.

anonymous function objects?

Is it possible to specify anonymous functions, something like:
f = {print "hello world"}
f()

hello world
in Pyton?
Lambda expressions don't work here.

Thanks,
Uwe

Jul 19 '05 #1
8 1599
Uwe Mayer <me*****@hadiko.de> writes:
f = {print "hello world"}
f()
hello world

in Pyton? Lambda expressions don't work here.


Closest you can come is:

f = lambda: sys.stdout.write("hello world\n")

Of course if you're trying to capture the function in a named variable
like f, just use a def statement.
Jul 19 '05 #2
Friday 29 April 2005 00:06 am Paul Rubin wrote:
Closest you can come is:

f = lambda: sys.stdout.write("hello world\n")
Ah. :))
Why does the "print" statement return a syntax error here?
lambda: print("hallo") File "<stdin>", line 1
lambda: print("hallo")
^
SyntaxError: invalid syntax
Of course if you're trying to capture the function in a named variable
like f, just use a def statement.


Unfortunately I want to assign a handler function to an object and something
like this does not work:
class Foobar(object): pass .... a = Foobar()
def a.handler():

File "<stdin>", line 1
def a.handler():
^
SyntaxError: invalid syntax

But wrapping print into a myprint function works :)
Thanks,
Ciao
Uwe
Jul 19 '05 #3
Uwe Mayer ha scritto:
Friday 29 April 2005 00:06 am Paul Rubin wrote:
Closest you can come is:

f = lambda: sys.stdout.write("hello world\n")

Ah. :))
Why does the "print" statement return a syntax error here?
^^^^^^^^^


this is the reason :)
You can't have statements into an expression
Jul 19 '05 #4
Uwe Mayer wrote:
Unfortunately I want to assign a handler function to an object and something
like this does not work:
class Foobar(object): pass
...
a = Foobar()
def a.handler():
File "<stdin>", line 1
def a.handler():
^
SyntaxError: invalid syntax


But this does work, or something close to it:
class Foobar(object): pass .... def handler(self): .... print 'in handler'
.... f = Foobar()
f.handler() Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'Foobar' object has no attribute 'handler'
import new
f.handler = new.instancemethod(handler, f)
f.handler()

in handler
-Peter
Jul 19 '05 #5
Uwe Mayer:
Why does the "print" statement return a syntax error here?


Google for "Python regrets" where Guido admits that
'print' should have been a function.

Michele Simionato

Jul 19 '05 #6
Friday 29 April 2005 05:40 am Michele Simionato wrote:
Uwe Mayer:
Why does the "print" statement return a syntax error here?


Google for "Python regrets" where Guido admits that
'print' should have been a function.


:) Will this change in future - and if not, why not?

Ciao
Uwe
Jul 19 '05 #7
On Thu, 28 Apr 2005 20:57:51 -0400, Peter Hansen <pe***@engcorp.com> wrote:
Uwe Mayer wrote:
Unfortunately I want to assign a handler function to an object and something
like this does not work:
>class Foobar(object): pass


...
>a = Foobar()
>def a.handler():


File "<stdin>", line 1
def a.handler():
^
SyntaxError: invalid syntax


But this does work, or something close to it:
class Foobar(object): pass... def handler(self):... print 'in handler'
... f = Foobar()
f.handler()Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'Foobar' object has no attribute 'handler'
import new
f.handler = new.instancemethod(handler, f)
f.handler()in handler


Or you can doctor up a Foobar that treats all
function attributes of its instances as methods
to be bound to the specific instance, e.g.,
(not tested beyond what you see)
class Foobar(object): ... def __getattribute__(self, attr):
... sentinel = object()
... atto = object.__getattribute__(self, '__dict__').get(attr, sentinel)
... if hasattr(atto, '__get__'): return atto.__get__(self, type(self))
... elif atto is sentinel: return object.__getattribute__(self, attr)
... return atto
... f = Foobar()
def handler(self): print 'handler of:', self ... f.handler = handler
f.handler() handler of: <__main__.Foobar object at 0x02F0428C> f <__main__.Foobar object at 0x02F0428C> f.handler <bound method Foobar.handler of <__main__.Foobar object at 0x02F0428C>> f.h Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 6, in __getattribute__
AttributeError: 'Foobar' object has no attribute 'h' f.h =123
f.h 123 f.f2 = lambda self: 'Hi'
f.f2 <bound method Foobar.<lambda> of <__main__.Foobar object at 0x02F0428C>> f.f2() 'Hi' Foobar.ordinary_method = lambda self: 'ordinary'
f.ordinary_method <bound method Foobar.<lambda> of <__main__.Foobar object at 0x02F0428C>> f.ordinary_method() 'ordinary'

Try another Foobar instance:
g = Foobar()
g.ordinary_method() 'ordinary' g.handler Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 6, in __getattribute__
AttributeError: 'Foobar' object has no attribute 'handler' def g_handler(self): print "g's own handler for", self ... g.alias = g_handler
g.alias() g's own handler for <__main__.Foobar object at 0x02EF134C>

BTW new.instancemethod(handler, f) seems to duplicate handler.__get__(f),
leaving out the type(f) in handler.__get__(f, type(f)) that would make a bound method,
which is what it is, and which could be set as attribute of anything.

Note the difference between newh which sticks with the f
passed to new.instancemethod, and real, which Foobar.__getattribute__
dynamically attaches to the selected instance:
import new
f.newh = g.newh = new.instancemethod(lambda self:('newh', self), f)
f.real = g.real = lambda self:('real', self)
f.newh() ('newh', <__main__.Foobar object at 0x02F0428C>) g.newh() ('newh', <__main__.Foobar object at 0x02F0428C>) f.real() ('real', <__main__.Foobar object at 0x02F0428C>) g.real() ('real', <__main__.Foobar object at 0x02EF134C>)
^^^^^^^^^^

IOW, a plain callable has __get__ and gets bound to the instance,
whereas a bound method doesn't have __get__ any more, so it gets
retrieved as a plain attribute. BTW, to get an ordinary function call
for a function attribute of a Foobar instance, you'd have to
eliminate the function's __get__ effect one way or another,
e.g. staticmethod (which really wraps the function with a descriptor,
substituting its own __get__, which Foobar.__getattribute__ uses transparently)
def sm(firstarg='sm first arg', *args): return firstarg, args ... g.sm = staticmethod(sm)
g.sm() ('sm first arg', ()) g.sm <function sm at 0x02EE8E9C> g.__dict__['sm'] <staticmethod object at 0x02E8177C> g.__dict__['sm'].__get__ <method-wrapper object at 0x02F0476C> g.__dict__['sm'].__get__(g)

<function sm at 0x02EE8E9C>

Regards,
Bengt Richter
Jul 19 '05 #8
I have not access to Guido's time machine!

M. S.

Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

76 posts views Thread by Nick Coghlan | last post: by
6 posts views Thread by Mark Brandyberry | last post: by
11 posts views Thread by G Fernandes | last post: by
10 posts views Thread by Frank O'Hara | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.