[NEWBIE]-Object referencing??

Steve Holden sholden at holdenweb.com
Tue Jul 2 08:08:25 EDT 2002


"Alistair Campbell" <campbells4 at ozemail.com.au> wrote in message
news:z8bU8.1944$yY2.74422 at ozemail.com.au...
> Hi,
>
> I have class defined as below
>
> <code>
> ...
> class Brew:
>
>     def __init__(self):
>         self.brew_id=""
>         self.brew_date=""
>         self.brew_type=""
>         self.hydro_init='1040'
>         self.hydro_final='1000'
>         self.starter=""
>         self.amt_start=""
>
>     def alcohol(self):
>         return
>
((string.atoi(self.hydro_init)-string.atoi(self.hydro_final))*0.00036251)*10
>
> ...
> <code>
>
> I assign data from a file so the data that goes into the alcohol method
> needs to be converted from string.
> The problem is that when I try to reference an instantiation of the Brew()
> class, i.e;
>
> current_brew=Brew()
>
> I can get all the other data items but a call to;
>
> current_brew.alcohol
>
> returns
>
> <bound method Brew.alcohol of <__main__.Brew instance at 0x01761250>
>
> So. I know that the calculation of alcohol content is not true but I am
just
> fiddling and will put in the correct formula when I get round to it.
>
> But, what am I doing wrong in relation to my definition or calling of the
> Brew.alcohol method?
>
> Trivial but nonetheless I have to start learning somewhere. Your help is
> much appreciated.
>
Alistair:

Now this is my kind of application - forget all that wimpy networking and
GUI stuff, let's get some alcohol brewing!

You've already been told that you have to actually *call* the alcohol()
method to get it to do the computation. I just though I'd ask if there was a
particular reason why the hydro_init and hydro_final attributes are stored
as strings?

If you converted them into numbers (presumably integers will do) when you
input them then you could simplify your alcohol method to

    def alcohol(self):
        return (self.hydro_init-self.hydro_final)*0.00036251*10

Also note that there's an int() built-in (actually it's a type in 2.2 and
on) that will convert a string into an integer for you. By the way, it seems
a little weird to not just multiply the difference by 0.0036251 -- is there
some reason for the separate multiplication by 10? Anyway, good luck with
the program. And remember, beer is *not* just a good breakfast.

a-balanced-meal-is-a-pint-in-each-hand-ly y'rs  - steve
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list