Python class factory ... or?
<p>We have a database library in C# that we can use like this:</p>

<pre><code>DatabaseConnection conn = DatabaseConnection.FromConnectionString("...");
</code></pre>

<p>this library hides many of the differences between different database engines, like sql function names, parameter names and specifications, etc.</p>

<p>Internally, the DatabaseConnection class is an abstract class implementing some of the basic methods, but the .FromConnectionString method runs through a list of registered specialized types that handles the actual differences, and constructs an object of the right class. In other words, I don't get a DatabaseConnection object back, I get a MSSQLDatabaseConnection or OracleDatabaseConnection object back instead, which of course inherit from DatabaseConnection.</p>

<p>The connection string contains information about what kind of database engine and version this connection is for.</p>

<p>I'd like to create a similar library in python.</p>

<p>Is the right approach to make something that can be constructed like this:</p>

<pre><code>conn = DatabaseConnection("...")
</code></pre>

<p>or through a class method:</p>

<pre><code>conn = DatabaseConnection.FromConnectionString("...")
</code></pre>

<p>is the first even possible, that is... constructing an object like this and getting back something else, a specialized object, depending on data in the passed string?</p>

<p>Ok, let me ask a different question... What is the <em>pythonic</em> way of doing this?</p>

<p>I basically want to have the DatabaseConnection base class in Python as well, implementing the common methods, and specialize in derived classes, and have a method or function somewhere that based on the connection string constructs and returns the right type of object.</p>
