An alternative approach to bound methods

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Thu Feb 22 14:14:49 EST 2001


Thu, 22 Feb 2001 17:30:24 +0100, Alex Martelli <aleaxit at yahoo.com> pisze:

> Where do you read/infer the limitation to *class* methods?

My proposal doesn't change how instance methods and instance attributes
can be used. Only class attributes change.

Or I didn't understand what you mean?

> But how does a given function, whose def is inside a class, know
> whether it's supposed to recurse through its (potential) overrider
> (via a same-name method of the first argument) or by defeating the
> override (via its bare name, or, equivalently if your proposal was
> adopted, using itself as an attribute in its enclosing class)?

Are you asking what should one choose when he is writing the class?
I don't know any case where it should recurse through its potential
overrider yet, but perhaps they exist. This question can't have a
general answer, similarly as questions like 'which class should I
make an instance of here'.

Are you asking about the semantics of a particular piece of code?
If so, which code? My proposal is unambiguous.

> Recursion apart, what distinguishes, e.g., a method foo in class
> Derived(Base), calling the version it overrides as Base.foo(self,x,y),
> and a "class-method" bar calling the same-name class-method of
> the base class as Base.bar(x,y,z)?

Technically nothing.

Ideologically foo is an instance method, and bar is a class method,
i.e. the first argument of foo is supposed to be an instance of a
subclass of Base (even Derived at this usage point).

Instance methods are special cases of class methods, where the
first argument is an object of the class we are in. They are not
distinguished by declaration: a method can play the role of both.

We have a shortcut for calling instance methods: instead of
    obj.__class__.method (obj, arg1, arg2)
you can also write
    obj.method (arg1, arg2)

It happens at the time of accessing methods from the instance, i.e.
    obj.method
itself means
    lambda *args, **kwargs: obj.__class__.method (obj, *args, **kwargs)
when type (obj) == types.InstanceType
         and not obj.__dict__.has_key ('method').

That's it.

-- 
 __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^                      SYGNATURA ZASTĘPCZA
QRCZAK



More information about the Python-list mailing list