[NEWBIE]-Object referencing??

Walter Dörwald walter at livinglogic.de
Wed Jul 3 08:05:52 EDT 2002


Alistair Campbell wrote:

> 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
> 0

This is simpler written as:
    return ((int(self.hydro_init)-int(self.hydro_final))*0.00036251)*100

Probably faster and no need to import the string module. (Provided
you're using Python 2.2)

> ...
> <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>

current_brew.alcohol is not a call to the alcohol method, it is the
alcohol method itself. When you want to call the method you always
have to specify the (), even if the method doesn't require arguments,
so current_brew.alcohol() will work.

> 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.

BTW, is there a reason, why hydro_init and hydro_final are strings
instead of int's?

> But, what am I doing wrong in relation to my definition or calling of the
> Brew.alcohol method?

Add () and it will work.

Bye,
    Walter Dörwald







More information about the Python-list mailing list