[Python-3000] PEP 3119 - Introducing Abstract Base Classes

Barry Warsaw barry at python.org
Tue May 1 01:06:11 CEST 2007

Hash: SHA1

On Apr 27, 2007, at 1:10 PM, Jim Jewett wrote:

> On 4/27/07, Barry Warsaw <barry at python.org> wrote:
>> - - Attributes.  Interfaces allow you to make assertions about
>> attributes, not just methods, while ABCs necessarily cover only  
>> methods.
> Why can't they have data attributes as well?

They can /have/ data attributes, but that's not really the point.   
The point (IMHO) is that such attributes can be documented,  
inspected, and reasoned about.  You could annotate interface  
attributes with type information in order to automatically generate  
database tables or web forms, etc.  Normal Python attributes can't do  
that, although if they were properties, they could.

>> - - With interfaces, you can make assertions about individual objects
>> which may be different than what their classes assert.  Interface
>> proponents seem to care a lot about this and it seems there are valid
>> uses cases for it.
> Isn't this something that could be handled by overriding isinstance?

It could.

>> Another example of separating inheritance and interface comes up when
>> you want to derive a subclass to share implementation details, but
>> you want to subtly change the semantics, which would invalidate an
>> ABC claim by the base class.  Something like a GrowOnlyDictionary
>> that derived from dict for implementation purposes, but didn't want
>> to implement __delitem__ as required by the MutableMapping ABC.
> OK, that makes the isubclass override trickier, so there should be an
> example, but I think it can still be done.
>> Finally, I'm concerned with the "weight" of adding ABCs to all the
>> built-in types.
> What if the builtin types did not initially derive from any ABC, but
> were added (through an issubclass override) when the abc module was
> imported?

That would allow for some unfortunately global side-effects.  Say I  
happen to import your library that imports abc.  Now all the built-in  
types in my entire application get globally changed.  I'm also not  
sure how you'd implement that.

- -Barry

Version: GnuPG v1.4.5 (Darwin)


More information about the Python-3000 mailing list