operations on types
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Jun 16 22:04:56 EDT 2015
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?
>> A different way would be to create (sensible) operations on types:
>>
>> NewType = OldObject + mixin #object composition
>>
>> Thoughts?
>
> 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.
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.
I think that this might be a bit more attractive in statically-typed
languages. If you wrote:
class NewType(OldType + Mixin): ...
the compiler could optimize away the intermediary class (OldType+Mixin)
and just use direct inheritance, but I don't think that would work in
Python. And a statically-typed language could avoid errors where you
forget to instantiate a class before doing regular addition:
result = OldType + x # oops, meant OldType()
--
Steven D'Aprano
More information about the Python-list
mailing list