D
Doran_Dermot
Hi All,
I've seen lots of code in which the attributes of a class are accessed and
modified using two separate methods. For example:
class Problems:
def __init__( self, refNum ):
self._refNum = refNum
self._title = ""
self._problem = ""
def setTitle( self, title="" ):
self._title = title
def setProblem( self, problem="" ):
self._problem = problem
def getTitle( self ):
return self._title
def getProblem( self ):
return self._problem
I prefer to use the following implementation that simulates function
overloading (an OOP feature that I like and that Python does not support):
class Problems:
def __init__( self, refNum ):
self._refNum = refNum
self._title = ""
self._problem = ""
def title( self, title=None ):
if title == None:
return self._title
self._title = title
def problem( self, problem=None ):
if problem == None:
return self._problem
self._problem = problem
This approach reduces the number of required accessor methods by 50% (bit of
an obvious statement I know). I also think that it this allows you to write
cleaner looking code. For Example:
theProblem = Problems( "12345" )
print "Title: ", theProblem.title()
print "Problem: ", theProblem.problem()
theProblem.title( "Java affecting adoption of Python." )
theProblem.problem( "Developers are more inclined to use Java instead of
Python due to the fact that Java is seen as a defacto standard." )
print "Title: ", theProblem.title()
print "Problem: ", theProblem.problem()
Am I correct in stating that Python does not support function overloading?
Have I been hiding under a rock and not noticed the thousands of examples
that illustrate this approach?
Any comments are welcome!
Cheers!!
Dermot Doran
EMC2 GTS Solutions Level 2
Office: +31-20-7768439
Mobile: +31-6-55815258
I've seen lots of code in which the attributes of a class are accessed and
modified using two separate methods. For example:
class Problems:
def __init__( self, refNum ):
self._refNum = refNum
self._title = ""
self._problem = ""
def setTitle( self, title="" ):
self._title = title
def setProblem( self, problem="" ):
self._problem = problem
def getTitle( self ):
return self._title
def getProblem( self ):
return self._problem
I prefer to use the following implementation that simulates function
overloading (an OOP feature that I like and that Python does not support):
class Problems:
def __init__( self, refNum ):
self._refNum = refNum
self._title = ""
self._problem = ""
def title( self, title=None ):
if title == None:
return self._title
self._title = title
def problem( self, problem=None ):
if problem == None:
return self._problem
self._problem = problem
This approach reduces the number of required accessor methods by 50% (bit of
an obvious statement I know). I also think that it this allows you to write
cleaner looking code. For Example:
theProblem = Problems( "12345" )
print "Title: ", theProblem.title()
print "Problem: ", theProblem.problem()
theProblem.title( "Java affecting adoption of Python." )
theProblem.problem( "Developers are more inclined to use Java instead of
Python due to the fact that Java is seen as a defacto standard." )
print "Title: ", theProblem.title()
print "Problem: ", theProblem.problem()
Am I correct in stating that Python does not support function overloading?
Have I been hiding under a rock and not noticed the thousands of examples
that illustrate this approach?
Any comments are welcome!
Cheers!!
Dermot Doran
EMC2 GTS Solutions Level 2
Office: +31-20-7768439
Mobile: +31-6-55815258