Converting between objects

Alex Popescu the.mindstorm.mailinglist at gmail.com
Fri Jul 20 05:33:28 EDT 2007


"Nathan Harmston" <ratchetgrid at googlemail.com> wrote in
news:676224240707191317i41e76ab7lceb39fcb981b0188 at mail.gmail.com: 

> 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

I think your question is somehow misleading and here is why I'm thinking 
this:
1/ on one side: creating objects from other objects (and this part is 
related to float(1), str(200) and have been covered by the others in 
this thread)
2/ on the otherside: making an object to behave differently or implement 
a new protocol described by the second object (I think this is the 
Pythonic way of calling it).

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




More information about the Python-list mailing list