operations on types

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jun 17 01:39:03 EDT 2015


On Wednesday 17 June 2015 12:42, Dr. John Q. Hacker wrote:

> On Tue, Jun 16, 2015 at 9:04 PM, Steven D'Aprano <
> steve+comp.lang.python at pearwood.info> wrote:
> 
>> On Tue, 16 Jun 2015 19:24:03 -0500, Dr. John Q. Hacker wrote:
>>
>> > [Dr. Bigcock wrote:]
>> >> The current syntax for adding functionality to a class using mix-in
>> >> style via inheritance list conflates two very different things.
>>
>> I'm not sure why you are taking "Dr Bigcock" seriously. You know he isn't
>> actually a doctor?
>>
> 
> Actually, I asked him.  He said he was has a PhD in Theology.

You believe somebody calling himself "Dr Bigcock" (among other pseudonyms)? 
Just how much credibility does somebody calling themselves Bigcock get? Even 
in the "Carry On" movies they didn't use any names as unsubtle as that.

https://www.youtube.com/watch?v=wcBOX1JBcjQ

Besides, I have a Doctorate of Divinity, and I'm not a doctor either.


>> > That's an awesome idea.  It's like a final way to understand and
>> > operate
>> > > with metaclasses.
>> >
>> > Unfortunately, it has nothing to do with metaclasses.
>>
> 
>  No, it *does*:  it's operations on classes themselves (i.e. types) and
>  not
> on *instantiations* of classes.  If that isn't metaclassing, then python's
> been calling it wrong.

Python's definition of "metaclass" (the class of a class) is the same as 
that used by (at least) Objective C and Smalltalk. If you're going to argue 
that *Smalltalk* is wrong, well, good luck with that, Smalltalk invented the 
concept.

http://www.cocoawithlove.com/2010/01/what-is-meta-class-in-objective-c.html

http://pharo.gforge.inria.fr/PBE1/PBE1ch14.html

If you think there's another definition of "metaclass" in widespread use, 
please give some citations or links.

What I will accept is that implementing class composition using the + 
operator *could* be implemented via the metaclass, at least in languages 
with metaclasses. But that's not the only way to do it. Java, for example, 
has no metaclasses (classes are not themselves objects in Java), but the 
Java language could easily define "Type + Type" as a way of composing types. 
It just wouldn't work via the metaclass.

That's what I mean by "it has nothing to do with metaclasses" -- you can 
implement this without using metaclasses.


>> I can see why:
>> >
>> >     NewType = OldType + Mixin
>> >
>> > appears more attractive at first glance than:
>> >
>> >     class NewType(OldType, Mixin):
>> >         pass
>> >
>> > since it saves a lot of boilerplate. But in practice, the body of the
>> > class is not often "pass". More often, you end up overriding or adding
>> > new methods. In that case, the suggested syntax has no advantage.
>>
> 
> Adding new methods is exactly what this syntax is supposed to do.

No, it creates a new class which inherits *existing* methods belonging to 
the two composed classes, OldType and Mixin. It doesn't create any methods 
that don't already exist.


> The tricky part is overriding existing methods. 

Precisely. If you want NewType.method to override either parent 
(OldType.method and/or Mixin.method) you need more than just a type 
composition operator. In Python you would use a class statement:

class NewType(OldType + Mixin):
    def method(self):
        ...


You would also do the same to add a new method not provided by either 
OldType or the Mixin.


-- 
Steve




More information about the Python-list mailing list