where to create the object

Emile van Sebille emile at fenx.com
Fri Mar 3 08:53:42 EST 2000


Hi Oivvio,

You could have it both ways and let the caller/user
decide:

    def getresult(self, inresult=None):
        if inresult == None:
            inresult = result()
        inresult.data = self.data * 5

This way, if the instance is passed , you can use it;
if not, you create it.

HTH,

Emile van Sebille
emile at fenx.com
-------------------


----- Original Message -----
From: Oivvio Polite <oivvio at cajal.mbb.ki.se>
To: Jason Stokes <jstok at bluedog.apana.org.au>
Cc: <python-list at python.org>
Sent: Friday, March 03, 2000 4:39 AM
Subject: Re: where to create the object


> Jason Stokes wrote:
> >
> > Oivvio Polite wrote in message
<38BF84E6.B52E7BCD at cajal.mbb.ki.se>...
> >
> > >the method  result of class foo generates a the result object bar.
> >
> > Ok....
> >
> > >the bar object could either created in the result method and
returned to
> > >the calling program.
> > >or it could be created in the calling program, supplied as an
argument
> > >to result that results just fills up with the right values.
> >
> > I'm afraid this is a little unclear.  Could you rephrase the
question, with
> > a code example?
> >
> > --
> > http://www.python.org/mailman/listinfo/python-list
>
>
> I'm afraid my code might be a little unclear as well :)
>
> This isn't in complete conformity with the specification in my first
> post, but you'll get the idea. In my real program I'll be generating a
> lot of results and I don't need to keep them for long. The result gets
> printed to a file immediately. Then I can throw it away and get the
next
> result. I think it is nicer to generate the result object in the
called
> method. But I'm worried about the penalities.
>
>
> ######################### first solution
> class foo:
>     def __init__(self, data):
>         foo.data = data
>
>     def getresult(self, inresult):
>         inresult.data = self.data * 5
>
>
> class result:
>     def __init__(self):
>         self.data = 0
>
> foo_object = foo(10)
> result_object = result() ##################    the resultobject is
> generated in the calling code
> foo_object.getresult(result_object)
> print result_object.data
> #will print 50
>
> ######################### second solution
>
> class foo:
>     def __init__(self):
>         foo.data = 0
>
>     def getresult(self):
>         newresult = result() #############     the result object is
> generated in the called method
>         newresult.data = self.data * 5
>         return newresult
>
>
> class result:
>     def __init__(self):
>         self.data = 0
>
> foo_object = foo()
> foo_object.data = 10
>
> result_object = foo_object.getresult()
> print result_object.data
> #will print 50
>
>
>
> --
> oivvio polite
> varvsgatan 10a / 117 29 stockholm
> mobil  0709 304030
> kontor 6696418
> hem    7919084
> fax    840018
> mailto:oivvio at polite.se
>
> --
> http://www.python.org/mailman/listinfo/python-list
>






More information about the Python-list mailing list