On Wed, Jul 20, 2011 at 4:37 AM, brandon w <thisisonlyat...@gmx.com> wrote:
> ** > Hi > I am running Linux with Python 2.6.6. I have done lists, tuples, > dictionaries, etc. Now I want to move on to creating a "class". I keep > getting an error for everything I try. Here is the error: * > > NameError: name 'MyClass' is not defined* > > I had originally tried to create my own class by watching some video > tutorials. Nothing worked. Then from the python2.6-doc documentation I just > decided to copy and paste from the documentation. > > *class MyClass: > """A simple example class""" > i = 12345 > def f(self): > return 'hello world'* > > > *>>> MyClass > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > NameError: name 'MyClass' is not defined* > > Still the same error. What am I doing wrong? > I tried this in gnome-terminal in a Python shell and using IDLE. > > First I do: *import myscript.py > *(no errors)* > * > Then I run: *MyClass* > (error) > > I try: n = *MyClass()* > (error) > > I try: > *MyClass.n > n.MyClass > i.MyClass > MyClass.i > i.MyClass() > f.MyClass > f.MyClass()* > > (nothing but errors) > You have to specify where MyClass lives. In this case it's in myscript.py. So you have to do: import myscript #Note: without .py n = myscript.MyClass() or: from myscript import MyClass n = MyClass() -- André Engels, andreeng...@gmail.com
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor