[Tutor] Dynamically changing a class

Kent Johnson kent37 at tds.net
Thu Sep 6 02:26:53 CEST 2007


Kent Johnson wrote:
> Jason Doege wrote:
>> I am building an API and for various reasons I have chosen Python to
>> implement it. I'd like to separate the implementation from the interface
>> as, for instance, C++ does with separate .hpp and .cpp files. Apart from
>> defining a class with a bunch of empty methods and then redefining them,
>> I have not seen a good way to do this in Python. Can you recommend the
>> Pythonic way to do this?
> 
> For smaller projects don't bother.

A bit more on this...it is common for Python APIs to be defined by 
convention, documentation and example rather than by code artifacts such 
as formal interfaces.

In Python itself, examples are file-like objects, sequences, mappings, 
iterators, decorators, descriptors and context managers, to name the 
ones that come to immediately mind. All of these are defined 
more-or-less clearly by documentation and usage.

A more extensive example is Python DB-API which defines a way to 
interface to a database. This is defined entirely by the spec:
http://www.python.org/dev/peps/pep-0249/

Kent



More information about the Tutor mailing list