[Tutor] "Overloading" methods
Vince Spicer
vince at vinces.ca
Thu Sep 16 16:22:56 CEST 2010
On Thu, Sep 16, 2010 at 6:02 AM, Michael Powe <michael at trollope.org> wrote:
> Hello,
>
> Strictly speaking, this isn't overloading a method in the way we do it
> in Java. But similar. Maybe.
>
> I am writing a module for processing web server log files and one of
> the methods I provide is to extract a given query parameter and its
> value. Because there are several types of log files with different
> line structures, I had the thought to write methods with descriptive
> names that simply returned a generic method that processed the method
> arguments. e.g.,
>
> def setpattern_iis(self,pattern,parameter) :
> type='iis'
> return pattern_generator(self,type,pattern,parameter)
>
> In this case, creating a regular expression to parse the log lines for
> a query parameter.
>
> This is just a bit more "self documenting" than using the generic
> method with the 'type' argument and requiring the user to enter the
> type. At the same time, it allows me to put all the parsing code in
> one method.
>
> My question is, is this a bad thing to do in python?
>
> Thanks.
>
> mp
>
> --
> Michael Powe michael at trollope.org Naugatuck CT USA
> War is a sociological safety valve that cleverly diverts popular
> hatred for the ruling classes into a happy occasion to mutilate or
> kill foreign enemies. - Ernest Becker
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
Well I can't comment on right or wrong I would think creating a simple class
with a __call__ method is a
little more pythonic.
Example:
class PatternGenerator(object):
"""Run a regex....."""
def __init__(self, type_):
self.type = type_
def __call__(self, pattern, parameter):
return pattern_generator(self, self.type, pattern, parameter)
# Initialize class
setpattern_iis = PatternGenerator('iis')
# call the method here
setpattern_iis("pattern", "params")
Hope that helps,
Vince
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100916/007f6875/attachment.html>
More information about the Tutor
mailing list