Function Overloading and Python
Allen Peloquin
tandonmiir at gmail.com
Mon Feb 25 02:48:28 EST 2008
On Feb 24, 11:44 pm, Stefan Behnel <stefan... at behnel.de> wrote:
> Allen Peloquin wrote:
> > class B
> > {
> > fun(A x, A y, A z)...
> > fun(A1 x, A y, A z)...
> > }
>
> > class B1
> > {
> > fun(A1 x, A y, A z)...
> > }
>
> > Such that any previous behavior is inherited, but behaves
> > polymorphically because of the single function name.
>
> Try something like this:
>
> class B(object):
> def fun(x,y,z):
> if isinstance(x, A1):
> return self._fun(x,y,z)
> # ...
>
> def _fun(x,y,z):
> # ...
>
> class B1(B):
> def _fun(x,y,z):
> # ...
>
> Stefan
The problem is that I want to reuse the code of the parent classes
through inheritance, otherwise this would work fine.
I am aware that because of the dynamic typing of Python, there
currently is no type-based function overloading, so I'm looking for
alternate solutions to my design problem.
More information about the Python-list
mailing list