jimjjewett at gmail.com
Tue Aug 7 00:55:00 CEST 2012
On 8/3/12, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Really, the phrase "optional abstract method" is a bit of an oxymoron.
Only if you place too much faith in the details on the current implementation.
One major use of an abstract class is to document an interface. The
abstract class can only define (or even document, really, if you value
docstrings) methods that it defines.
> For interface methods that aren't mandatory, there are already a
> few different signalling methods currently in use within the language
> core and standard library:
> 1. Attribute checks
> Call method X if it exists, call method Y otherwise (e.g. the
> iterator protocol, which falls back to the sequence iterator if
> __iter__ doesn't exist, but __getitem__ does, and the backwards
> compatibility in the path entry finder protocol, which tries
> find_loader first, then falls back to find_module)
Assuming that another method will be called is a bit too restrictive,
but this otherwise works well -- until you inherit from the abstract
class, in which case the method will exist, even if it wasn't
The least bad solution I've found is to use co-operative super, and
write an identity function for the base class. Then if you need to
know whether or not it was actually implemented, you can at least do
an identity check on the method, if you're careful enough.
More information about the Python-ideas