473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Converting between objects

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
Jul 19 '07 #1
3 1202
Nathan Harmston wrote:
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

--
BOFH excuse #55:

Plumber mistook routing panel for decorative wall fixture

Jul 19 '07 #2
"Nathan Harmston" <ra*********@googlemail.comwrites:
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.
str(200)
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)

--
\ "Pinky, are you pondering what I'm pondering?" "Wuh, I think |
`\ so, Brain, but if we didn't have ears, we'd look like weasels." |
_o__) -- _Pinky and The Brain_ |
Ben Finney
Jul 19 '07 #3
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).
Jul 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mario Crevits | last post by:
My name is Mario Crevits, I'm from Belgium (Roeselare) and I'm working with Access97 for several years now. We are in an Access97-2000 migration project. I'm writing a wizard for the end-users to...
2
by: John Wilson | last post by:
I have an application that can access data both locally and through web services (it may be working in a disconnected mode). I have defined my data objects (e.g. ObjectA) in a separate component...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
9
by: jdlists | last post by:
I have inheirted some existing code, that i will explain in a moment, have needed to extend and ultimately should be able to run in threads. I've done a bunch of work with python but very little...
4
by: --CELKO-- | last post by:
I need to convert a bunch of DB2 triggers to Oracle. Is there any kind of tools for this?
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.