Defining class files

Steve Holden sholden at holdenweb.com
Thu Mar 29 15:22:21 CEST 2001


"Neil Benn" <neil.benn at cambridgeantibody.com> wrote in message
news:3ac33343$0$12248$ed9e5944 at reading.news.pipex.net...
> Hello,
>
>             Sorry to ask such a basic question but -
>
> 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??
>
>     Any/all help would be most appreciated!!
>
The confusion here appears to be about namespaces. Since you now have a
YourClass module, which defines a YourClass class, the correct way to
instantiate (create an instance of) the class would be:

    y = YourClass.YourClass()

Here the module name is qualified by the name of the object.

Does this help?

regards
 Steve






More information about the Python-list mailing list