global name 'self' is not defined

John Machin sjmachin at lexicon.net
Sat Dec 2 15:56:23 EST 2006


Evan wrote:
> In answer to the 2 replies, I had no references anywhere to 'self'.  In
> order to post my code I rewrote 2 scripts containing just the relevant
> parts of the problem; these work.  However, they are identical to my
> original code.

This is (putting it mildly) somewhat difficult to believe. If true, it
would indicate a rather severe bug in Python. Identical as determined
how?

When you ran your original code and it gave an error, Python would have
told you where the error occurred, on which line of which file, as in
the following example:

C:\junk>copy con noself.py
def foo():
   return self
^Z
        1 file(s) copied.

C:\junk>\python25\python
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
| >>> import noself
| >>> noself.foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "noself.py", line 2, in foo <<<=== location of error-causing
line
    return self <<<=== contents of error-causing line
NameError: global name 'self' is not defined

And you could have told us that information. No, should. Adding to what
others have already said:

When asking a question about an error message:
(a) Provide code (abbreviated if necessary) that causes the error.
Don't retype it; copy/paste the code that you actually ran.
(b) Show the full traceback and error message. Again, use copy/paste.

> So I have deleted the 'old' script2 and renamed the new
> one, and no problem.  I don't know why it worked with one and not the
> other when they are identical, but I have what I want now.

No problem? Sorry, it just transformed itself. Here is a precise
definition of the transformed problem: "I don't know why it worked with
one and not the other".

And what you want now doesn't include enlightenment? Thrashing madly at
problems with a sledgehammer may sometimes (but not always) make them
appear to go away faster than a methodical problem-solving approach
would take, but it's rather a short-tem "gain".

HTH,
John




More information about the Python-list mailing list