[maemo-developers] New project idea, pygtk questions, and a toolkit
From: Marius Gedminas marius at pov.ltDate: Tue Feb 20 13:31:28 EET 2007
- Previous message: New project idea, pygtk questions, and a toolkit
- Next message: Natural language - finnish?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Mon, Feb 19, 2007 at 12:41:15PM -0500, Sean Luke wrote:
> - Why is it in python that you can attach a function, and an instance
> variable, to an instance, but you cannot attach a method?
I am not sure I understand your question. Are you asking why functions
assigned to instance attributes do not get passed the hidden 'self'
argument?
There's a distinction between functions, unbound methods and bound
methods. When you assign a function to a class attribute, you get a
unbound method.
>>> class MyBtn(object):
... pass
...
>>> def foo(*args):
... print args
>>> foo
<function foo at 0xb7d5f1ec>
>>> MyBtn.foo = foo
>>> MyBtn.foo
<unbound method MyBtn.foo>
When you access the attribute of the instance, the method gets bound to
the instance
>>> btn = MyBtn()
>>> btn.foo
<bound method MyBtn.foo of <__main__.MyBtn object at 0xb7c1438c>>
When you call a bound method, it calls the underlying function with all
the arguments you passed, plus the 'self' argument stored in the bound
method itself.
>>> btn.foo(1, 2)
(<__main__.MyBtn object at 0xb7c1438c>, 1, 2)
When you assign a function to an instance attribute, there's no magic
and you have a function, not a method
>>> btn.bar = foo
>>> btn.bar
<function foo at 0xb7d5f1ec>
>>> btn.bar(1, 2)
(1, 2)
If you want to get a 'self' argument, you have to pass it yourself
>>> btn.bar = lambda *args: foo(btn, *args)
>>> btn.bar(1, 2)
(<__main__.MyBtn object at 0xb7c1438c>, 1, 2)
> Or is
> there a mechanism I'm not aware of? Second: can you make an
> anonymous function through any route than a lambda?
If by "anonymous" you mean "a function that does not have a name", then
you can't. You can define a named function in the middle of another
function.
> Because lambdas,
> weirdly, don't permit statements inside them. In NewtonScript (and
> Self, and JavaScript I think) objects are just dictionaries -- as is
> the case for Python underneath -- and you can do this:
>
> myButton := {
> _proto: protoButton,
> printme: func() begin
> print("hi there");
> print("I am " & self) end
> };
>
> I'd like to do this in python with the following equivalent (but
> invalid) syntax:
>
> myButton= Button()
> myButton.printme = lambda self:
> print("hi there")
> print("I am " + self)
myButton = Button()
def printme(self=myButton):
print "hi there"
print "I am", self
myButton.printme = printme
> Can someone more experienced in Python tell me why this can't be
> done, or if it can be done, how?
Marius Gedminas
--
question = 0xFF; // optimized Hamlet
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.maemo.org/pipermail/maemo-developers/attachments/20070220/7792f82d/attachment.pgp
- Previous message: New project idea, pygtk questions, and a toolkit
- Next message: Natural language - finnish?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
