beginner question: extending python types

Sean 'Shaleh' Perry shalehperry at attbi.com
Mon May 13 17:26:44 EDT 2002


On 13-May-2002 Uwe Mayer wrote:
> Hi,
> 
> I am totally new to python and was about to start programming when more 
> and more questions upon "how to do it actually" came up. 
> 
> My problem: I want do write classes which behave like algebraic 
> structures, i.e. a group, a field or vector space and then want to work 
> on them.
> 

have you looked at Numeric?  Lots of this stuff has been implemented before. 
Now if you are looking to implement it so you can learn, fine.

> To my first question: 
> I know I can overwrite built-in operations like add with, f.e. __add__() 
> methods, but can I add new f.e. postfix operators like "!" (facculty: n! 
> = 1*2*3*4*...*n)?`
> 

you can not define new operators (most overloading systems do not allow this).
n.factorial() or answer = factorial(n).

> To my last question for now:
> I want to force certain parameters of methods to be of a specific type, 
> so that when I expect a set I'll get a set or a sub-class thereoff. How 
> to I controll that? Use the type() function and raise an IllegalType 
> exception otherwise?
> 

yep.





More information about the Python-list mailing list