Converting between objects

N

Nathan Harmston

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
 
B

Bjoern Schliessmann

Nathan said:
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.

Yes, simply write something like

class a(object):
def __init__(self, parameter):
if isinstance(parameter, B):
# conversion from B to a
else:
# something else

Something like that is called "conversion constructor". At least in
C++.

Regards,


Björn
 
B

Ben Finney

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

float(1)

Calls the constructor for the 'float' type, passing the integer 1; the
constructor returns a new float object.

Calls the constructor for the 'str' type, passing the integer 200; the
constructor returns a new str object.
is there way I can define conversion functions like this

These are not "conversion functions"; they are the constructors of the
corresponding types.

You can hook into the instance creation by modifying the '__new__'
method of the metaclass; but probably the easier way is to hook into
the instance initialisation (after it is created) by modifying the
'__init__' method of the class.
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()

Rather than this, define '__init__' to do some stuff to 'self', that
is, modify attributes of the instance during its initialisation. The
'__init__' method is automatically called during object initialisation
and receives all the parameters that were passed to the constructor.

class B(object):
def __init__(self, obj):
self.foo = do_stuff(obj)

aobj = B(42)
 
B

Bruno Desthuilliers

Nathan Harmston a écrit :
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.

Just about the "not very OO" part: in Python, a class is a callable
object - just like functions - that when called usually returns an
instance of itself. IOW, a class is a Factory. So, from the client code
POV, the fact that the factory is a class or function (both being
callable objects) doesn't make any difference. Remember that OO is about
objects, not about classes.
Please correct me if I m wrong or offer me hints as to a better way to
do it ?

Others already answered the question. Note that if you need the factory
to be able to return different subtypes based on it's argument or on
some 'external' condition (platform, settings, whatever), you can also
override the class '__new__' method, which is the real constructor
(__init__ being the initialiser).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top