[Python-ideas] A proper way to bring real interfaces to Python

Steven D'Aprano steve at pearwood.info
Sat May 4 23:36:35 EDT 2019


On Sun, May 05, 2019 at 04:23:58AM +0300, Serge Matveenko wrote:
> Hi, all!
> 
> I believe, almost everybody is familiar with the `abc` package.
> 
> The problem is that the ABC class is not a separate thing from
> `object`. So, using ABCs often results in complicated inheritance
> designs and even in the infamous metaclass conflict.
[...]
> So, I would like to propose adding a third main object called
> `interface` in addition to `object` and `type` and to use it to define
> interface objects. Such interfaces could then be used in the class
> definition in the following way.

How will that solve the problem? Your `interface` object will still 
inherit from both object and type since everything inherits from object 
and all types inherit from type.


> Another problem is that ABC performs checks at the moment an object is
> being instantiated which isn't exactly the way one expects an
> interface to work.

Isn't it? That's how I expect it to work in Python, and I haven't had 
any problems with it so far.

That's not to say there aren't problems, but you should explain what 
they are rather than assume that others have experienced the same issues 
you have. Why is the late check a problem? Are you worried about 
performance?

It isn't that I *oppose* moving the checks to class-creation time 
instead of instantiation time, but I'd like to hear more about why it is 
a problem.


> The obvious way would be to enforce the
> implementation at the moment `type` for that class is created, i.e. on
> module execution time.

Module execution time is not necessarily when the class is created, if 
it matters.



-- 
Steven


More information about the Python-ideas mailing list