[Tutor] classes question

Magnus Lycka magnus@thinkware.se
Tue Oct 22 16:23:01 2002


At 21:33 2002-10-22 +0300, Karthikesh Raju wrote:
>               self.k =3D k
>               self.n =3D n
...
>                     self.data =3D RandomArray.random([K,N])

Is this deliberate? Are K and N global variables?
Python is case sensitive, K is not the same thing as k.

>def __add__(self, other):
>      return source(self.data + other)

I suppose this method definition was indented as it should.
As written here, it's not part of the class. It's always
a bit difficult to judge edited code snippets like this.
Posting a minimal but runnable program is always good. Then
we know whether oddities are part of the program or just a
consequence of editing an email.

>i keep getting errors when i try x+5; x is an object of type source.

Exact error messages are helpful too.

>i want to be able to implement
>
>x+5
>x+y (x,y are of type sources)
>
>and other non source types should be converted to source types.

Can you please write how addition of source objects
should work mathematically? What you coded was along
the lines that:

If x is source and y is integer, z =3D x + y =3D> z is a
new source objects instanciated with k =3D x.data + y,
n =3D 1, type =3D 'uniform'.

This won't work since k should be an integer, and
x.data is an array.

A few style issues: It's common practice to capitalize
class names, e.g. Source. It's also common practice to
avoid hiding builtin functions like you do when you call
a variable 'type'. If you need to use the type() function
for some test in init, you need to rename the parameter
type, which is bad, since it's part of the interface of
the class. (You can get around this with something like
"python_type =3D type" before the class definition, and then
using python_type() inside the source class, but that is
a bit awkward. Breaking these "rules" isn't illegal in
python but it usually makes life simpler if we comply...

I imagine you might want something like:

class source:
     def __init__(self, k=3D1, n=3D1, type=3D'uniform'):
         self.k =3D k
         self.n =3D n
         self.type =3D type
         if type =3D=3D 'uniform':
             self.data =3D RandomArray.random([k,n])
         else:
             pass # What???
     def __add__(self, other):
         if isinstance(other, source):
             assert (self.k=3D=3Dother.k) and (self.n=3D=3Dother.n)\
                    and (self.type=3D=3Dother.type))
             result =3D source(self.k, self.n, self.type)
             result.data =3D self.data + other.data
             return result
         elif type(other) =3D=3D type(0):
             result =3D source(self.k,self.n, self.type)
             result.data =3D self.data + other
             return result
         else:
             raise TypeError, "Only integer or source can be added to=
 source"
     # Assuming a+b =3D=3D b+a
     __radd__ =3D __add__



--=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