An interesting way to implement an abstract base class

olsongt at verizon.net olsongt at verizon.net
Fri Aug 4 13:59:27 EDT 2006


This one made me smile.  From:

http://aima.cs.berkeley.edu/python/utils.html#Queue

class Queue:
    """Queue is an abstract class/interface. There are three types:
        Stack(): A Last In First Out Queue.
        FIFOQueue(): A First In First Out Queue.
        PriorityQueue(lt): Queue where items are sorted by lt, (default
<).
    Each type supports the following methods and functions:
        q.append(item)  -- add an item to the queue
        q.extend(items) -- equivalent to: for item in items:
q.append(item)
        q.pop()         -- return the top item from the queue
        len(q)          -- number of items in q (also q.__len())
    Note that isinstance(Stack(), Queue) is false, because we implement
stacks
    as lists.  If Python ever gets interfaces, Queue will be an
interface."""

    def __init__(self):
        abstract

    def extend(self, items):
        for item in items: self.append(item)




More information about the Python-list mailing list