Re: Why can't I run this test class?
On Fri, Sep 11, 2009 at 4:01 AM, Kermit Mei wrote: > On Fri, 2009-09-11 at 00:43 -0700, Chris Rebert wrote: > > On Fri, Sep 11, 2009 at 12:40 AM, Kermit Mei > wrote: > > > On Fri, 2009-09-11 at 00:33 -0700, Chris Rebert wrote: > > >> On Fri, Sep 11, 2009 at 12:30 AM, Kermit Mei > 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 "", line 1, in > > > TypeError: 'module' object is not callable > > > > You've imported the module `Test`, whose name is determined by the > > filename (Test.py). > > To access the class of the same name (`Test`) that is defined in the > > module, you need to use the dot operator: > > > > import Test > > t1 = Test.Test() > > > > You should probably use different names for the module/file and the > > class to avoid confusion. > > Unlike Java, Python does not observe a direct correspondence between > > filenames and classes. > > > > Cheers, > > Chris > > -- > > Oh, yep! > Thanks, Cheers. > > Can you tell me how can I write __init__.py for modules: > > I have a directory like this: > $ tree > . > `-- main >|-- MyTestModules >| |-- Test1.py >| `-- Test2.py >`-- main.py > > 2 directories, 3 files > > > In main.py, I want to run the following code: > > #!/usr/bin/env python > > import MyTestModules > > t1 = Test1() > t2 = Test2() > > print t1.first() > print t2.first() > > ### > The classes Test1 and Test2 just be similar with the Test that I showed > before. To run main.py correct, how can I orgnize the code under > directory MyTestModules. (May need a __init__.py file under > MyTestModules, but I don't know how to write it) > > > Thank you,very much! > Regards > Kermit > > Just make a blank file called __init__.py in MyTestModules. That's all you need. Then, in main.py, you need to either do import MyImportModules.Test1 t1 = MyImportModules.Test1.Test1() or from MyImportModules.Test1 import Test1 t1 = Test1() Unlike C++ and Java, an import in Python does not make the other module's stuff available in the local namespace. You only have the one extra name (the name of the module) and everything else is stored in there. If you want to add the new module's stuff to the local namespace, you have to do the "from ... import ...". This is also why you shouldn't make a separate file for every class. You could put both Test1 and Test2 into a file called test.py (module names in Python are usually lowercase) and then do "from MyImportModules.test import Test1, Test2" > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Why can't I run this test class?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Why can't I run this test class?
On Fri, 2009-09-11 at 00:43 -0700, Chris Rebert wrote: > On Fri, Sep 11, 2009 at 12:40 AM, Kermit Mei wrote: > > On Fri, 2009-09-11 at 00:33 -0700, Chris Rebert wrote: > >> On Fri, Sep 11, 2009 at 12:30 AM, Kermit Mei 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 "", line 1, in > > TypeError: 'module' object is not callable > > You've imported the module `Test`, whose name is determined by the > filename (Test.py). > To access the class of the same name (`Test`) that is defined in the > module, you need to use the dot operator: > > import Test > t1 = Test.Test() > > You should probably use different names for the module/file and the > class to avoid confusion. > Unlike Java, Python does not observe a direct correspondence between > filenames and classes. > > Cheers, > Chris > -- Oh, yep! Thanks, Cheers. Can you tell me how can I write __init__.py for modules: I have a directory like this: $ tree . `-- main |-- MyTestModules | |-- Test1.py | `-- Test2.py `-- main.py 2 directories, 3 files In main.py, I want to run the following code: #!/usr/bin/env python import MyTestModules t1 = Test1() t2 = Test2() print t1.first() print t2.first() ### The classes Test1 and Test2 just be similar with the Test that I showed before. To run main.py correct, how can I orgnize the code under directory MyTestModules. (May need a __init__.py file under MyTestModules, but I don't know how to write it) Thank you,very much! Regards Kermit -- http://mail.python.org/mailman/listinfo/python-list
Re: Why can't I run this test class?
On Fri, Sep 11, 2009 at 12:40 AM, Kermit Mei wrote: > On Fri, 2009-09-11 at 00:33 -0700, Chris Rebert wrote: >> On Fri, Sep 11, 2009 at 12:30 AM, Kermit Mei 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 "", line 1, in > TypeError: 'module' object is not callable You've imported the module `Test`, whose name is determined by the filename (Test.py). To access the class of the same name (`Test`) that is defined in the module, you need to use the dot operator: import Test t1 = Test.Test() You should probably use different names for the module/file and the class to avoid confusion. Unlike Java, Python does not observe a direct correspondence between filenames and classes. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Why can't I run this test class?
On Fri, 2009-09-11 at 00:33 -0700, Chris Rebert wrote: > On Fri, Sep 11, 2009 at 12:30 AM, Kermit Mei 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 "", line 1, in TypeError: 'module' object is not callable >>> Thanks Kermit -- http://mail.python.org/mailman/listinfo/python-list
Re: Why can't I run this test class?
On Friday 11 September 2009 09:30:42 Kermit Mei wrote: Do this: class Test(object): > t1 = Test And this: t1 = Test() That makes an instance and runs the __init__ \d -- home: http://otherwise.relics.co.za/ 2D vector animation : https://savannah.nongnu.org/projects/things/ Font manager : https://savannah.nongnu.org/projects/fontypython/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Why can't I run this test class?
On Fri, Sep 11, 2009 at 12:30 AM, Kermit Mei 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 -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Why can't I run this test class?
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 print t1.first() # But when I run it, some errors take: $ ./Test.py Traceback (most recent call last): File "./Test.py", line 13, in print t1.first() TypeError: unbound method first() must be called with Test instance as first argument (got nothing instead) What I want is as the following c++ code do: class Test { public: Test() :arg1(1) {/*Do nothing*/} int first() const { return arg1; } protected: int arg1; }; void main(void) { Test t1; std::cout << t1.first; } Hope your help. Thanks Kermit -- http://mail.python.org/mailman/listinfo/python-list