Python – Various Programming Concepts

language-agnosticobject-orientedpython

Ever since I've started lurking on stackoverflow, I keep coming across programming concepts such as abstract classes, virtual functions, contracts, interfaces, etc., from a questions about other languages such as C++, Java, and C#.

As far as I can tell, most of these things are related to object oriented programming but aren't typically mentioned in Python tutorials because Python uses a combination of duck typing and magic to make such concepts redundant.

Nevertheless, I'd still like to know what they are.
What are abstract classes, virtual functions, contracts, interfaces, etc. (explained from a Python standpoint?) What other programming/OOP concepts should I know about that Python typically doesn't use or require you to know?

Best Answer

Abstract classes

Abstract classes are classes where some methods on the class are missing implementations. Python can (sort of) have these, but they're not quite as explicit as languages like Java, where the program will refuse to build. Typically, you inherit from an abstract class, and fill in the missing pieces, possibly using non-abstract methods from the abstract base class. Consider this class, in Python:

class Foo(object):
    def bar(self):
        # code code code

    def baz(self):
        raise NotImplementedError()

This class is kind of like an abstract class: we can't (realistically) call baz. (We can, but we'll get only exceptions.) Unlike Java or C++, where an abstract class is one that can't be instantiated, we can instantiate Foo. Python provides the abc module if you want more "abstract" classes.

Virtual Functions

Virtual functions are functions that, when called from a reference or pointer to a base class, will call the derived version of that function. In C++, if I have:

Foo *foo = ClassDerivedFromFoo();
foo->bar();

If bar isn't virtual, Foo::bar will get called. If it is, ClassDerivedFromFoo::bar will get called, if it exists. (The most-derived version will get called.)

In Python, an over-simplification would be that one can think of all functions as being virtual. This isn't quite correct: an instance's functions are really just attributes on that instance, and you can easily "monkey-patch" them to be an arbitrary function.

Contract

A contract is an agreement between a function and a caller of a function as to things that happen during the execution of a function. It's essentially the guarantees a function makes. To my knowledge, none of Python/Java/C++ enforce these explicitly. See Design by contract.

Interface

In a loose sense, an interface is the API (note that the "I" is interface) some piece of code (a module, class, function, etc.) exposes: it consists of what function calls are available, what types of data do they take and return, and what should that data be composed of.

Java has a special meaning for interface: An interface is a class-like description that contains member functions that contain no body. A class that contains all of those functions then, "implements" that interface. The interface can be used as a base class of sorts.

Python has similar, but much more abstract concepts. Take the enumerate function. It takes an "iterable", and returns the items from that iterable paired with increasing integers. (If you feed it ['a', 'b', 'c'] it spits out (0, 'a'), (1, 'b'), (2, 'c'). My example was a list, but it'll happily take any iterable: sets, dictionaries, generators, tuples, etc. As long as the item meets the interface of an iterable. This interface isn't (in Python) recorded explicitly, but as long as the object you pass in acts like an iterable, everything should just work. (This is duck typing.)

The Java "interface" idea is closely related to the general idea of abstract base classes, and some of the things the abc module can do might be interesting in regards to interfaces, as well.