Why can't I run this test class?

Kermit Mei kermit.mei at gmail.com
Fri Sep 11 09:40:02 CEST 2009


On Fri, 2009-09-11 at 00:33 -0700, Chris Rebert wrote:
> On Fri, Sep 11, 2009 at 12:30 AM, Kermit Mei <kermit.mei at gmail.com> wrote:
> > Dear all,
> >    I'm a newbie for python, and I write a program to test how to
> > implement a class:
> >
> > #!/usr/bin/env
> > python
> >
> > class Test:
> >    'My Test class'
> >    def __init__(self):
> >        self.arg1 = 1
> >
> >    def first(self):
> >        return self.arg1
> >
> > t1 = Test
> 
> You missed the parentheses to call the constructor. That line should be:
> 
> t1 = Test()
> 
> Cheers,
> Chris


Yes, that can run. But If I put the following code into Test.py :
#!/usr/bin/env python                                               |>>>
                                                                    |
class Test:                                                         |
    'My Test class'                                                 |
    def __init__(self):                                             |
        self.arg1 = 1                                               |
                                                                    |
    def first(self):                                                |
        return self.arg1                                            |
                                                                    |
    def setFirst(self,value = 5):                                   |
        self.arg1 = value   

But when I want to run it as a module, something also be wrong:

$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Test
>>> t1 = Test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>> 

Thanks

Kermit





More information about the Python-list mailing list