Defining class files

Simon Brunning SBrunning at trisystems.co.uk
Thu Mar 29 08:22:37 EST 2001


> From:	Neil Benn [SMTP:neil.benn at cambridgeantibody.com]
> I'm wokring through the tutorial and have reached the section on classes
> in
> Python.  Working through the example on classes I have entered the class
> using the interpreter:-
> 
> class MyClass:
>     "A simple example class"
>     i = 12345
>     def f(x):
>         return 'hello world'
> 
>     This worked fine when I typed:-
> 
> >>> x=MyClass()
> >>> x.f()
> 'Hello World'
> 
>     However, I then tried to write the class as an external text file,
> using:-
> 
> class YourClass:
>     "A simple example class"
>     i = 12345
>     def f(x):
>         return 'hello world'
> 
>     I then imported the class, tried to assign the class and ivoke a
> method
> :-
> 
> >>> y = YourClass()
> Traceback (innermost last):
>   File "<console>", line 1,
> TypeError: call of non-func
> 
>     This seemed a bit strange, so I investigated the Your class and it
> seemed to be a module.
> 
> >>> YourClass
> <module YourClass at 2584541>
> 
>     The text file is saved as YourClass.py - is this the problem, should
> class files have different terminaters in their filename??
 
Assuming that you imported the module like this:

import YourClass

you need to qualify the class name, as so:

y = YourClass.YourClass()

the unqualified label YourClass refers to the imported module, not the class
within it.

HTH

Cheers,
Simon Brunning
TriSystems Ltd.
sbrunning at 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.




More information about the Python-list mailing list