User Tools

Site Tools


python:misc:class_factory

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
python:misc:class_factory [2015/12/26 11:14]
ozan [Python Class Factory]
python:misc:class_factory [2016/05/22 11:21] (current)
Line 158: Line 158:
 Output is as expected so it works as our factory class. This demonstration is just for showing how ''​%%__new__%%''​ method works so I do not prefer to use it as a factory. Output is as expected so it works as our factory class. This demonstration is just for showing how ''​%%__new__%%''​ method works so I do not prefer to use it as a factory.
 </​wrap>​ </​wrap>​
-<WRAP center round info 60%+ 
-TO BE CONTINUED... + 
-</WRAP>+==== Example 3: Using Dict ===== 
 + 
 +<wrap indent
 +This is an easy solutionUse a dictionary holding the classes and return the appropriate class as can be seen from the example below. 
 +</​wrap>​ 
 +<code python>​ 
 + 
 +conn_dict = {"​mysql":​ ConnectionMysql,​ "​sqlite":​ ConnectionSqlite} 
 + 
 +connection = conn_dict["​mysql"​](username="​username",​ password="​password",​ host="​192.168.1.10") 
 +</code> 
  
 {{tag>​python class_factory __new__ __init__ inheritance}} {{tag>​python class_factory __new__ __init__ inheritance}}
 ~~DISCUSSION~~ ~~DISCUSSION~~
python/misc/class_factory.txt · Last modified: 2016/05/22 11:21 (external edit)