Defining class files

Fredrik Lundh fredrik at pythonware.com
Thu Mar 29 08:29:20 EST 2001


Neil Benn wrote:
> 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'

that's a module containing a class, not a "class file"

as you noticed, "import YourClass" gives you the module.

to get at the class itself, refer to it as "YourClass.YourClass"
(the YourClass object in the YourClass module).

you can also write "from YourClass import YourClass".

> The text file is saved as YourClass.py - is this the problem, should
> class files have different terminaters in their filename??

there are no such thing as a "class file" in Python.  just modules...

for more info, see chapter 6 in the tutorial:

    http://www.python.org/doc/current/tut/node8.html

Cheers /F





More information about the Python-list mailing list