[Tutor] Basic question -> How to use a class from a file?

Bernard Lebel python at bernardlebel.com
Mon Jan 3 00:01:34 CET 2005


All righty, thanks a lot!


Bernard


tanja pislar wrote:
> hi Bernard,
> 
> you have to specify the module as well:
> 
> let's say your module is called rtModule.py,
> then in your case you'd do:
> 
> import rtModule
> 
> testClass = rtModule.rt()
> testClass.walk()
> 
> or:
> from rtModule import rt
> 
> testClass = rt()
> testClass.walk()
> 
> regards,
> tanja
> 
> 
> On Sun, 02 Jan 2005 17:43:07 -0500, Bernard Lebel
> <python at bernardlebel.com> wrote:
> 
>>Hello,
>>
>>An easy one to start the year.
>>
>>Trying to write my first class, already running into problems after 3
>>lines! :-(
>>
>>So have this little class:
>>
>>class rt:
>>        def walk(self):
>>                print 'yeah'
>>
>>So if I write this in the Python shell, instantiate rt and call walk(),
>>I get the proper result. yeah gets printed.
>>
>>Now let say I put this into a file. I want to import that class and use
>>its methods, but this is were I get lost.
>>
>>import rt
>>rt.walk()
>>
>>Returns:
>>Traceback (most recent call last):
>>   File "<interactive input>", line 1, in ?
>>AttributeError: 'module' object has no attribute 'walk'
>>
>>When I try to instantiate the class, I get an non-callable module object
>>error.
>>
>>Sooooo how to use a class from a file?
>>
>>Thanks
>>Bernard



More information about the Tutor mailing list