function overloading

John Roth johnroth at ameritech.net
Tue Jul 1 16:03:17 EDT 2003


"Simon Burton" <simonb at webone.com.au> wrote in message
news:pan.2003.07.01.19.42.06.202399 at webone.com.au...
>
> I just wrote the following rediculous code.
> What do people do when they need to overload like this?
>
> Simon.
>
>
>
> ######################################################
> # PIL stuff
>
> class Im:
>   def __init__(self,x,xx=None):
>     if xx is not None:
>       w,h = x,xx
>       im = Image.new("I",(w,h))
>     elif type(x)==str:
>       filename = x
>       im = Image.open(filename)
>     else:
>       im = x
>     self.im = im
>

I'd subclass it. I see three classes here:

class Image:
    def __init__(self, oldImage)

class NewImage(Image):
    def __init__(self, height, width):

class ImageFromDisk(Image):
    def __init__(self, fileName):

The subclasses only override the __init__ method
of the base class.

An alternate method of dealing with the situation
is to do lazy initialization. In other words, don't provide
any parameters on the construction, and provide three
different methods for initializing the resulting object.

John Roth






More information about the Python-list mailing list