Why can't I run this test class?

Ulrich Eckhardt eckhardt at satorlaser.com
Fri Sep 11 12:14:26 CEST 2009


Kermit Mei wrote:
> #!/usr/bin/env
> python
> 
> class Test:
>     'My Test class'
>     def __init__(self):
>         self.arg1 = 1
> 
>     def first(self):
>         return self.arg1
> 
> t1 = Test

't1' is now an alternative name for 'Test'. What you wanted instead was to
instantiate 'Test', which you do with this syntax:

 t1 = Test()


> print t1.first()

't1.first' or 'Test.first' is a function that takes a single argument (by
convention, that should be an instance of 'Test'). However, no such
instance is provided:

> TypeError: unbound method first() must be called with Test instance as
> first argument (got nothing instead)


Note that you can invoke the 'first' function in two ways:

  t1.first()
  Test.first(t1)


Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list