P
paul
DependsÒ»Ê×Ê« said:Consolidate existing functions?
I've thought about it.
For example, I have two functions:
#=========================
def startXXX(id):
pass
def startYYY(id):
pass
#=========================
I could turn it into one:
#=========================
def start(type, id):
if(type == "XXX"):
pass
else if(type == "YYY"):
pass
#=========================
But isn't the first style more clear for my code's user?
There are more ways to structure code than using classes. To avoid the
if-elif-elif-elif-else problem you could start using a dispatch table
which maps types to functions (fex using a dict)
start_methods = {
'type1': startXX,
'type2': startYY,
}
def start(type, id):
func = start_methods.get(type, None)
if func:
func(id)
else:
raise ...
Or maybe look at trac's (http://trac.edgewall.com) use of Components and
Interfaces. Very lightweight and modular. You can start reading here:
http://trac.edgewall.org/browser/trunk/trac/core.py
cheers
Paul