[Tutor] Strange class behavior

Simon Brunning SBrunning@trisystems.co.uk
Thu, 30 Nov 2000 16:38:47 -0000


> From:	Darrell Brogdon [SMTP:dbrogdon@valinux.com]
> Why is it that when I run the following script:
> 
> class myTestClass:
>     def myTestMethod(self):
>         print "Test, Test, 1-2-3"
> 
> 
> b = myTestClass
> b.myTestMethod()
> 
> 
> I get the following error:
> 
> Traceback (most recent call last):
>   File "./test2.py", line 9, in ?
>     b.myTestMethod()
> TypeError: unbound method must be called with class instance 1st
> argument
> 
> 
> Clearly I'm doing something wrong with the method definition but I've
> not been able to determine what?
 
Your method definition is fine. The problem is with the line where you
instantiate your object, or rather, don't. You need this:

b= myTestClass()

rather than:

b= myTestClass

The latter creates new reference to the myTestClass class, rather than
creating on object belonging to that class.

Cheers,
Simon Brunning
TriSystems Ltd.
sbrunning@trisystems.co.uk





-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.