[Tutor] Re: class trouble (Kalle Svensson + Andrei Kulakov)

Johannes Graumann graumann@its.caltech.edu
Sat, 20 Jul 2002 01:33:11 -0700 (PDT)


Kalle Svensson wrote:
[SNIP]
>> print ypr123a.length
>
>A bit unexpected, maybe.  Methods and functions are objects as any other
>in Python, so this just prints the object. <method porf.length of porf
>instance at 0x810d79c> If you add parantheses to invoke the method, this
>is the result.
>>>> print ypr123a.length()
>46
>> print ypr123a.stats('a')
>6 What errors are you experiencing? Peace,

None other ;0) - see below.

Andrei Kulakov wrote:
[SNIP]
><method porf.length of porf instance at 0x810775c>
>6
>Instead of length you probably want __len__(self) - then you can do
>len(instance) and it'll run that function.
>And to run the length function you have, do print ypr123a.length(). The

>4th statement is correct, I think.. Isn't it?
Yes.

Thanks! You know, second day python, stolen the setup from all kinds of
sources and then I bang my head for HOURS against a simple '()' ... ;0)
Is there any particular advantage to the '__len__(self)' solution?

Cheers and good night!

Johannes