[Tutor] classes question

Karthikesh Raju karthik@synapse.hut.fi
Wed Oct 23 13:23:01 2002


Hi Magnus and all pythoners,

On Tue, 22 Oct 2002, Magnus Lycka wrote:

> 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])
>=20
> Is this deliberate? Are K and N global variables?
> Python is case sensitive, K is not the same thing as k.

It was a mistake. Actually what i did was a very fast writeup into the
mail. k,n are the number of users and number of blocks peruser. K,N are
not global variables.

What i wanted was to convert all my matlab code into python for more than
speed reasons. So, i wanted to write a class that generated sources. i
just used uniform as a first hack. It should generate sources with
different distributions (eg: uniform, gaussian, rayliegh etc
etc). All these data are compliant to mathematical operations
(add/sub/mul/div), and if matrix then all linear algebra operations

The first trick was to try addition: and i got struck there

>=20
> >def __add__(self, other):
> >      return source(self.data + other)
>=20
> 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.

It is a part of the class, and i was overloading the basic operations

>=20
> >i keep getting errors when i try x+5; x is an object of type source.
>=20
> Exact error messages are helpful too.
>=20
Sothing to the  likes of this cant be done .....

> 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'.
>=20
> This won't work since k should be an integer, and
> x.data is an array.
>=20
> 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
...

i didnt know i was breaking these rules (too much of matlab is bad for
programming :-( ). Type could have been even distribution or anything. i
was an error of ignorance :-)

> I imagine you might want something like:
>=20
> 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 s=
ource"
>      # Assuming a+b =3D=3D b+a
>      __radd__ =3D __add__
>=20
>=20

i think this is quite close. The only issue here is=20
>>>  result =3D source(self.k, self.n, self.type)
This generates unnecessary data which is then replaced with the actual
data. Any methods to reasonably do it otherways.

Actually, since i come from a non-classy world, is my approach right.

Build up source classes, build class of detectors glue them together alon=
g
with the channels class..

best regards
karthik



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