Converting between objects

Nathan Harmston ratchetgrid at googlemail.com
Thu Jul 19 22:17:38 CEST 2007


Hi,

I have being thinking about this and was wondering with built in types
you can do things like

float(1) or str(200)

is there way I can define conversion functions like this:

say i have a class A and a class B

bobj = B()
aobj = a(bobj)

in a neater way than just defining a set of methods

def a(object_to_convert)
    # if object_to_convert of type......
    # do some stuff
   return A()

def b(object_to_convert)
    # if object_to_convert of type......
    # do some stuff
   return B()

Cause this seems a little verbose and not very OO.

Please correct me if I m wrong or offer me hints as to a better way to do it ?

Many Thanks

nathan



More information about the Python-list mailing list