Re: Voluntary Abstract Base Classes



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/viewpost.jsp?thread=208549 where he
mentions ABC's but don't quite understand what the whole story is
about.

Anyone has good use cases?

Daniel

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):


def is_slice_like(candidate):
for att in 'start', 'stop', 'step', 'indices':
if not hasattr(candidate, att):
return False
return True

def doit(anarg):
if not is_slice_like(anarg):
raise Exception, 'Must give something like a slice.'
do_rest_of_it()


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
class MySlice(SliceLike):
# etc.

def doit(anarg):
if not issubclass(anarg.__class__, SliceLike):
raise Exception, 'Must give something SliceLike.'

def main():
anarg = MySlice(1,2,3)
doit(anarg)


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.

James
.



Relevant Pages

  • Re: A subquery? Not sure if this is doable?
    ... the weekly series and the repeating monthly figure. ... 1/12/05 ABC 4.22 ... 1/12/05 DEF 3.12 ... 1/12/05 XYZ 8.88 ...
    (microsoft.public.access.queries)
  • Re: Classes as units of reuse
    ... Component ABC { ... The thing that is not clear in your pseudo code is the nature of the relationship between ABC and DEF. ... IMO, the second approach would be the best where one abstracts some set of objects with the proper functionality and then decouples their details from the rest of the application via a Facade interface to which the clients all talk. ... In the OO paradigm one basically has three levels of logical indivisibility: subsystem, object, and responsibility. ...
    (comp.object)
  • Re: A subquery? Not sure if this is doable?
    ... frequency such that the table with less frequency will repeat its value ... 1/12/05 ABC 4.22 ... 1/12/05 DEF 3.12 ... 1/12/05 XYZ 8.88 ...
    (microsoft.public.access.queries)
  • Re: A subquery? Not sure if this is doable?
    ... my computer hangs and hangs and hangs. ... 1/12/05 ABC 4.22 ... 1/12/05 DEF 3.12 ... 1/12/05 XYZ 8.88 ...
    (microsoft.public.access.queries)
  • Re: Linking sort results
    ... ABC 123 ... DEF 234 ... to cater for the max likely rows of data in cols A and B ... except that it points to col E in Sheet1 ...
    (microsoft.public.excel.worksheet.functions)