classes question!

Robin Munn rmunn at pobox.com
Wed Oct 16 11:23:39 EDT 2002


In article <2dfzv6y2g4.fsf at synapse.hut.fi>, Karthikesh Raju wrote:
> 
> Hi,
> 
> i just started to write a class, when i was struck:
> 
> i did something like:
> 
> 
> import Numeric
> import RandomArray
> 
> class source:
>       def __init__(self, k=1,n=1,type='uniform'):
>               self.k = k
>               self.n = n
>               self.type = type
>               if type == 'uniform':
>                     self.data = RandomArray.random([K,N])

First question: what happens if type is not 'uniform'? Then self.data
does not get set, and you'll get an AttributeError when you try to use
self.data later on.

Second question: why are you using lowercase k in your method and
uppercase K in your call to RandomArray.random()? That'll get you a
NameError from the Python interpreter -- Python is case-sensitive,
unlike (say) Visual Basic.

Third question: what do k and n stand for, anyway? It might be obvious
to you but it's not obvious to me. Use more descriptive variable names.

>       
> Now i want to do __add__ overloading and the function should return an
> another source
> 
> i did:
> 
> def__add__(self, other):
>      return source(self.data + other)

I assume this was not cut&pasted from your code. If it was, you'd have
gotten a SyntaxError -- you need a space between 'def' and '__add__'.

> 
> i keep getting errors when i try x+5; x is an object of type source. 
> 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.

Well, when you do x+5, Python calls x.__add__(5) -- which is the same as
calling source.__add__(x, 5). If you don't understand the preceding
sentence, go read section 9.3.4 of the Python tutorial:

    http://www.python.org/doc/current/tut/node11.html#SECTION0011340000000000000000

until you do. Now: when you call source.__add__(x, 5), your function
tries to return a source object initialized from x.data+5. x.data has
been initialized from the RandomArray.random() function.

Fourth question: what does the RandomArray.random() function return? And
does *that* object have its behavior defined when you try to add an
integer to it? For example, a list can't have an integer added to it;
Python will give you a TypeError if you try.

Fifth question: what happens when you do x+y where x and y are both
source objects? (I know the answer to this one, but you'll learn more if
you answer it yourself :-))

Sixth question: did you also want to be able to do 5+x? As your code
stands now, you'll get an error. Read:

    http://www.ibiblio.org/obp/thinkCSpy/app02.htm

and think about the difference between __mul__ and __rmul__. Then think
about the difference between __add__ and __radd__. (And while you're
thinking, hit the up-arrow link on that page and read the whole book!)

> 
> Any help is great,
> 
> thankx in advance,
> karthik
> 

Hope this gets you pointed in the right direction to understand operator
overloading better.

-- 
Robin Munn
rmunn at pobox.com



More information about the Python-list mailing list