[Tutor] Re: Re: Factory classes (etc)

Magnus Lycka magnus@thinkware.se
Sun, 08 Sep 2002 10:57:15 +0200


At 21:21 2002-09-06 -0400, Arthur wrote:
>I am not "missing" method overloading in the sense that I feel Python is
>somehow deficient by not having it built-in.
...
>But for what I am doing I want to build as much as I can a scripting
>interface that follows geometric intuitive logic, not programmers logic.
>And at the scripting interface level what things are called *is* to the
>essence
>of things. If a point is instantiated as an Intersection, if might be the
>intersection of a number of other possible elements. It is absolutely
>redundant
>both to have the Intersection classes named in accordance with what=
 elements
>they take as arguments, and then to give them those arguments - because in
>fact the arguments themselves define the kind of intersection we are
>trying to create.

It's really difficult to help you when you are so abstract.
To be honest, I don't really understand what you are trying
to do. I think it would be easier if you showed a sample of
code.

The thing is, that if you have the same name, you should
basically do the same thing. In Java or C++ you can't to
the same thing just once if the types or number of params
differ, but in Python you usually can.

import math, cmath, operator

# This handles any number of ints, floats or complex numbers.
def RootSumSquared(*args):
     if type(1j) in map(type, args): sqrt =3D cmath.sqrt
     else: sqrt =3D math.sqrt
     return sqrt(reduce(operator.add, map(lambda x: x*x, args)))

RSS =3D RootSumSquared

print RSS(3,4)
print RSS(0.3, 0.4)
print RSS(3j, 4)
print RSS(0.3, 4, 5j)
print apply(RSS, (1, )*100)

Of cource the simple solution in Java or C++ would be to
cast all arguments to Complex, but note that I don't use
the slower complex sqrt unless I have a complex parameter.

Would your overloaded method look very different from one
another? Maybe you can warp your thinking into making them
much more similar, and then further into being the same
thing?

>Given lets say 20 classes which could take anywhere from one to
>five different sets of arguments, and thats a hell of a lot of
>if type(arg[0]):
>    xxx
>else:
>    yyyy
>
>to try to get right and keep straight.

I bet it will still be much shorter than Java if you use
the kind of signature recognition function that I mentioned
previously...

BTW, would it be possible to write a base class Overloader
such that:

class X(Overloader):
     def add_int_int(self, a, b):
         return a + b
     def add_string_int(self, a, b):
         return a + str(b)

a =3D X()

a.add(1, 3) =3D> 4
a.add('a', 5) =3D> 'a5'
a.add('d', 'd') =3D> AttributeError

__getattr__ will catch the calls to the undefined
'add' method, but I don't know how to figure out
what the argument list looked like. Can that be
found from the Python introspection magic?

 >>> class X:
...     def x(self):
...             print "Hello"
...     def __getattr__(self, value):
...             return self.x
...
 >>> a =3D X()
 >>> a.hello()
Hello
 >>> a.bitMap()
Hello

This is a little bit on the way, it the __getattr__
is in the base class. There are seemingly useful
functions in the inspect module, but they aren't so
easy to figure out...



--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se