[Tutor] why "self" in methods?
orbitz at ezabel.com
orbitz at ezabel.com
Mon Apr 5 09:01:41 EDT 2004
Taking no self argument is not the same as unbound. unbound just means the
method is not bound to an instance. Take Danny's example of calling super
class's method.
>From Nevow's code:
class Renderer(ChildPrefixMixin, resource.Resource, RenderFactory, DataFactory):
def __init__(self, original=None):
# Some other junk here...
resource.Resource.__init__(self)
Using unbound method to call the constructor of one of the parent classes.
On Mon, 5 Apr 2004 11:25:17 +0100
"Alan Gauld" <alan.gauld at blueyonder.co.uk> wrote:
> > Yes: if one needs to call methods of a superclass, one way to do it
> is
> > with an explicit call to the "unbound" method of the superclass.
> Here's a
> > completely useless toy example:
> >
> > ###
> > >>> class SomeClass(object):
> > ... def sayHello(self):
> > ... print "hi, I'm SomeClass"
> > ...
>
> But sayHello is not unbound in the sense mentioned in the OP
> (which actually may not be the right term!). What I was really
> asking was what use (if any) do methods without a self parameter have?
>
> Python allows you to create them but, so far as I can tell,
> you can never use them?
>
> ie:
>
> >>> class SomeClass(object):
> ... def sayHello(self):
> ... print "hi, I'm SomeClass"
> ... def noSelf():
> ... print "I'm selfless"
> ...
> >>> print SomeClass.sayHello
> <unbound method SomeClass.sayHello>
> >>> print SomeClass.noSelf
> <unbound method SomeClass.noSelf>
>
> So far they look the same.
>
> >>> s = SomeClass()
> >>> print s.sayHello
> <bound method SomeClass.sayHello of <__main__.SomeClass object at
> 0xa06672c>>
> >>> print s.unboundMeth
> <bound method SomeClass.unboundMeth of <__main__.SomeClass object at
> 0xa06672c>>
>
> And they still look similar after binding
>
> >>> s.sayHello()
> hi, I'm SomeClass
> >>> s.noSelf()
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: noSelf() takes no arguments (1 given)
>
> But when you try to call them....
>
> Now it might be sensible to tread noSelf as a class method
> - ie one which operates on the class level rather than the
> instance level...
>
> >>> SomeClass.noSelf()
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: type object 'SomeClass' has no attribute 'noSelf'
> >>>
>
> But nope, you can't do that either.
>
> So is there any way to use these? And if not why does Python happily
> allow them? It should be possible to detect parameterless function
> definintions within a class scope surely? (The only reason I can
> think of might be to allow definition of higher order methods
> - ie methods which return functions which may not take
> parameters.... but even that should be detectable...)
>
> Just curious,
>
> Alan G.
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list