Checking object types

Alex Popescu nospam.themindstorm at gmail.com
Wed Aug 1 13:59:41 EDT 2007


"Robert Dailey" <rcdailey at gmail.com> wrote in
news:496954360708011012p20f2dd3cu74f0ff34deb5505f at mail.gmail.com: 

> ------=_Part_51775_19953536.1185988361742
> Hi,
> 
> I'm currently interested in creating an __add__() operator for one of
> my classes. This class handles both integers and objects its own type,
> however I don't know how I can perform special add operations
> depending on which is passed in. Since I haven't seen any evidence of
> function overloading, I'm assuming I'll have to check the types of the
> variables passed in inside of my __add__() method.
> 
> Take the two following examples:
> 
> vector3(3,4,2) + 5 = vector3(8,9,7)
> vector3(3,2,1) + vector3(4,5,6) = vector3(7,7,7)
> 
> Any tips? Thanks.
> 

That's an option. Another option was posted a while ago by GvR and was 
using some decorator to dispatch to different methods according to the 
type. I don't seem to find the link right now, but you can search it on GvR 
blog.

bests,
./alex
--
.w( the_mindstorm )p.




More information about the Python-list mailing list