Using function parameters to determine method kind

Lenard Lindstrom len-1 at telus.net
Tue Apr 6 17:34:22 EDT 2004


michele.simionato at poste.it (Michele Simionato) writes:

> Lenard Lindstrom <len-1 at telus.net> wrote in message
>                              news:<8yh9r7p2.fsf at telus.net>...
> > Michele Simionato <michele.simionato at poste.it> wrote:
> > >Lenard Lindstrom <len-1 at telus.net> wrote in message
> > > news:<4qrzym6e.fsf at telus.net>...
> > >> I was wondering if anyone has suggested having Python determine
> > >> a method's kind from its first parameter.
> >  ...
> > >I find relaying on tbe parameter name to specify the kind of method
> > >to be rather fragile. It works most times but not always. What about
> > >inner classes for instance?
... 
> No, I had in mind this (contrived) example:
> 
> class Outer(object):
>       def outermeth(self1):
>           class Inner(object):
>                 def innermeth(self2):
>                     <do-something-with-self1-and-self2>
>           ...
> 
> It is nice to have the ability to give any name to "self".
Yes, I see what you mean. The best I can suggest is:

class Outer(object):
      def outermethod(self1):
          class Inner(object):
                def innermeth(self2):
                    ...
                innermeth = instancemethod(innermeth)
      outermeth = instancemethod(outermethod)

or
      def outermethod(self):
          self1 = self
          ...   def innermethod(self):
                    self2 = self

I believe there is a use for a builtin instancemethod descriptor anyways.
It can wrap callables not of type python function.

>
...
> > Descriptors are unaffected by the function's parameters, so the first
> > parameter can be anything you want. If I am not mistaken, the only
> > methods other that __new__  that currently work without descriptors
> > are instance methods. 
> 
> ?? All methods works with descriptors, what do you mean?
>
By descriptor I mean wrapper classes like staticmethod. A naked function
can only be an instance method descriptor, which by convention has
a first parameter of 'self'. __new__ is exceptional in that it is an
instance method that is always called as an unbound method. I see though
that __new__ would break if accidentally made a class method.

> > Is it not good form for instance methods to
> > start with 'self' anyways? (except maybe for a metaclass's __init__,
> > but this is a special case)
> 
> It is not a special case (no special case is special enough or something
> like that).
I thought a metaclass's __init__ was a special case because it was called
from the meta meta-class, usually by method type.__call__. So maybe it
could be called as an unbound method to avoid problems if it was not
an instance method. But isn't type.__call__ the factory function for
all new-style objects, not just classes? I also overlooked cooperative
__init__ calls for multiple metaclass inheritance. So yes, there is nothing
special about a metaclass's __init__ other than it is usually declared
with 'cls' rather than 'self' as a first parameter. This is enough to
convince me that a method's first argument is not a reliable
indicator of method kind.

All this talk about python functions just being another kind
of descriptor makes me wonder if it is not time to revive
the idea of having type function subclassable:

class C(object):
    def f(c):
        __factory__ = classfunction # No confusion here
        ...

issubclass(classfunction, type(lambda: None)) # True
isinstance(C.f.im_func, classfunction) # True

:-)

Thanks for answering my questions.

Lenard Lindstrom
<len-l at telus.net>



More information about the Python-list mailing list