classes and sub classes?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Apr 9 03:08:02 EDT 2013


On Tue, 09 Apr 2013 07:50:11 +0200, Morten Guldager wrote:

> 'Aloha Friends!
> 
> I'm about to write an API against a huge propitiatory Oracle based
> network inventory database. The database have many different concepts
> stored in it's tables, can one concept can span over multiple tables.
> 
> I would like to write a class for accessing each concept, but only have
> a single database connection throughout the whole program.

Sounds reasonable.


> I imagine some code along these lines, but cant figure out how to
> declare the classes that will make it work:
> 
> # create a connection to the database and perform come basic login and
> initialization
> nib = NwInvDb("scott/tiger at ora")
> # find a device by ip
> interesting_device = nib.Device.lookup_by_ip("192.168.1.1")

What's "nib" mean? And "NwInvDb"? I can imagine that the "Db" at the end 
stands for Database, but the rest is just word-salad. I can guess that 
NwInvDb is some sort of database connection. Am I close?


> In this example I access the concept Device.
> 
> Should I make the Device class inherit from NwInvDb? Or should I keep
> them separate? 

Why are you asking us? We don't know what functionality you expect NwInvDb 
and Device to have, what they represent, or whether a Device can be 
meaningfully considered an instance of a NwInvDb, whatever that is.

But given my *guess* that NwInvDb represents a database connection, and 
that Device represents data fetched from that database, then no of course 
you should not inherit. Inheritance implies an "is-a" relationship. If 
you inherit from NwInvDb for Device, that implies:

- interesting_device Is-A database;

- anywhere you can use a NwInvDb database object, you can use 
  a Device object.

And the same would apply to every other concept in the database.

That does not sound like a clean and useful design to me.


-- 
Steven



More information about the Python-list mailing list