Daniel Nogradi wrote:
> Hi list,
> Well, the short question is: what are they? I've read Guido's python
> 3000 status report on
> http://www.artima.com/weblogs/viewpo...?thread=208549 where he
> mentions ABC's but don't quite understand what the whole story is
> Anyone has good use cases?
My interpretation of his description is that this is a way to check for
quack likes a duck behavior.
Say I want a function to check if something behaves like a slice, but
don't want to resort to checking for explicit inheritence, I might now
have to do something like (factoring out the checking):
for att in 'start', 'stop', 'step', 'indices':
if not hasattr(candidate, att):
if not is_slice_like(anarg):
raise Exception, 'Must give something like a slice.'
However, with an ABC, you could define what it means to be slice-like by
defining a slice-like ABC (e.g. SliceLike) and then virtually inheriting
from this ABC:
# assuming abstract inheritence syntax is the same as regular
if not issubclass(anarg.__class__, SliceLike):
raise Exception, 'Must give something SliceLike.'
anarg = MySlice(1,2,3)
With ABCs, the concept of slice-like is more formalized and transparent
than the attribute checking done in is_slice_like and is far more
flexible than explicit type checking.
The concept seems to be borrowed from Java interfaces.
But I'm ready to be corrected on my interpretation.