global name 'self' is not defined

Discussion in 'Python' started by Evan, Dec 2, 2006.

  1. Evan

    Evan Guest

    Hi

    I have a short script that makes 2 calls to methods in another script
    as follows:

    import canPlaces as canp

    callOne=canp.addMe(3,5)
    callTwo=canp.estocStn()

    The 2 methods in the second script are:

    def addMe(a,b)
    sumAb=a+b
    return sumAb

    def estocStn():
    estoc={'lat':29.15,'lon':-15.667,'place':'ESTOC'}
    return estoc

    Why is it that the first call works fine, but the second tells me
    'global name 'self' is not defined'? What I want is to have the
    dictionary 'estoc' available in my calling script.

    Thanks, Evan
     
    Evan, Dec 2, 2006
    #1
    1. Advertising

  2. Evan wrote:
    > Hi
    >
    > I have a short script that makes 2 calls to methods in another script
    > as follows:
    >
    > import canPlaces as canp
    >
    > callOne=canp.addMe(3,5)
    > callTwo=canp.estocStn()
    >
    > The 2 methods in the second script are:
    >
    > def addMe(a,b)
    > sumAb=a+b
    > return sumAb
    >
    > def estocStn():
    > estoc={'lat':29.15,'lon':-15.667,'place':'ESTOC'}
    > return estoc
    >
    > Why is it that the first call works fine, but the second tells me
    > 'global name 'self' is not defined'? What I want is to have the
    > dictionary 'estoc' available in my calling script.
    >
    > Thanks, Evan


    Please post examples that reproduce the error; what you posted doesn't
    even refer to "self" at all.

    George
     
    George Sakkis, Dec 2, 2006
    #2
    1. Advertising

  3. On 2 Dec 2006 10:42:28 -0800, Evan <> wrote:
    > Why is it that the first call works fine, but the second tells me
    > 'global name 'self' is not defined'? What I want is to have the
    > dictionary 'estoc' available in my calling script.


    Well, you have not posted the code that is causing the problem,
    nowhere in your mail there's a reference to "self".


    --
    Felipe.
     
    Felipe Almeida Lessa, Dec 2, 2006
    #3
  4. Evan

    Evan Guest

    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. 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.

    Thanks for your replies.

    -Evan
     
    Evan, Dec 2, 2006
    #4
  5. Evan

    John Machin Guest

    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
     
    John Machin, Dec 2, 2006
    #5
  6. Evan wrote:

    > So I have deleted the 'old' script2 and renamed the new one, and
    > no problem.


    Pity. Next time try using diff (or something similar).

    Regards,


    Björn

    --
    BOFH excuse #115:

    your keyboard's space bar is generating spurious keycodes.
     
    Bjoern Schliessmann, Dec 3, 2006
    #6
  7. Evan

    Evan Guest

    The problem seems to be with ipython, which I have been using to run
    these scripts. My calling script (call.py) is:

    import canaryPlaces_test as canp

    sum=canp.addme(3,5)
    print sum
    estoc=canp.estocStn()
    print estoc



    The problem script is this one, named canaryPlaces_test.py:

    def estocStn():
    estoc={'lat':29.15,'lon':-15.667,'place':'ESTOC'}
    return estoc

    def addme(a,b):
    sumAb=a+b
    return sumAb



    The ok script, named canaryPlaces.py, is identical apart from a comment
    at the top of one of them. Here is output from 'diff' for the two of
    them:

    @suse212:~/python/mapping> diff canaryPlaces_test.py canaryPlaces.py
    1c1
    <
    ---
    > # canaryPlaces

    evan@suse212:~/python/mapping>



    >From the command line I get what I expect calling either

    canaryPlaces_test.py or canaryPlaces.py:

    @suse212:~/python/mapping> python call.py
    8
    {'lat': 29.149999999999999, 'place': 'ESTOC', 'lon': -15.667}
    evan@suse212:~/python/mapping>



    However -> Using ipython and calling canaryPlaces.py is fine, but
    calling canaryPlaces_test.py gives:

    In [97]: %run call
    8
    ---------------------------------------------------------------------------
    exceptions.NameError Traceback (most
    recent call last)

    /home/evan/python/mapping/call.py
    2
    3 sum=canp.addme(3,5)
    4 print sum
    ----> 5 estoc=canp.estocStn()
    6 print estoc
    /home/evan/python/mapping/canaryPlaces_test.py in estocStn()
    5 return estoc
    6
    7 def addme(a,b):
    8 sumAb=a+b
    9 return sumAb
    NameError: global name 'self' is not defined
    WARNING: Failure executing file: <call.py>

    In [98]:


    On my system this error is repeatable.

    -Evan








    Bjoern Schliessmann wrote:
    > Evan wrote:
    >
    > > So I have deleted the 'old' script2 and renamed the new one, and
    > > no problem.

    >
    > Pity. Next time try using diff (or something similar).
    >
    > Regards,
    >
    >
    > Björn
    >
    > --
    > BOFH excuse #115:
    >
    > your keyboard's space bar is generating spurious keycodes.
     
    Evan, Dec 4, 2006
    #7
  8. On 4 Dec 2006 04:25:29 -0800, "Evan" <> declaimed the
    following in comp.lang.python:

    > The problem seems to be with ipython, which I have been using to run
    > these scripts. My calling script (call.py) is:
    >

    One: Does ipython generate/use an equivalent to Python's .pyc/.pyo
    files.

    Two: have you deleted any such file, so the import is forced to
    rebuild them (if the date stamps/magic number/whatever somehow got out
    of sync, it may not be importing what you think it is).

    > import canaryPlaces_test as canp
    >
    > sum=canp.addme(3,5)
    > print sum
    > estoc=canp.estocStn()
    > print estoc
    >
    >

    Three: Have you tried embedding the test logic at the bottom of the
    imported module (if __name__ == "__main__": do the tests) and trying to
    run those directly rather than importing.

    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Dec 4, 2006
    #8
  9. Evan

    Evan Guest

    Hi Dennis, to answer your questions:

    1) So far as I can see ipython generates .pyc files.
    2) This morning I ran the scripts, and got the same problem using
    ipython as in my earlier post. I then deleted the .pyc file, ran the
    calling script and this time it works. I then replaced the .pyc file I
    had deleted, expecting to have the problem again, but no.
    3) I've tried your suggestion, and it works fine.

    A few questions: Why does python use the double underscore (__main__ or
    if __name__)? I've only been using python for about 3 weeks, and I see
    this syntax a lot, but haven't found an explanation for it so far?
    Also, as I understand , the .pyc files should be updated every time you
    change and run the equivalent .py file?

    Thanks, Evan
     
    Evan, Dec 5, 2006
    #9
  10. Evan wrote:

    > A few questions: Why does python use the double underscore (__main__ or
    > if __name__)? I've only been using python for about 3 weeks, and I see
    > this syntax a lot, but haven't found an explanation for it so far?


    to quote the language reference, "System-defined names. These names are
    defined by the interpreter and its implementation (including the
    standard library)". see:

    http://effbot.org/pyref/reserved-identifier-classes

    </F>
     
    Fredrik Lundh, Dec 5, 2006
    #10
  11. On 5 Dec 2006 02:27:01 -0800, "Evan" <> declaimed the
    following in comp.lang.python:

    > A few questions: Why does python use the double underscore (__main__ or
    > if __name__)? I've only been using python for about 3 weeks, and I see
    > this syntax a lot, but haven't found an explanation for it so far?
    > Also, as I understand , the .pyc files should be updated every time you
    > change and run the equivalent .py file?
    >

    Bottom up:

    .pyc/.pyo are generated during an import IF the interpreter, after
    checking various magic numbers in the header and date-stamps determines
    that the .py is newer. If something glitched your clock, it is possible
    that an old .pyc still looks newer and gets pull in. There have also
    been cases of the Python installer (on Windows, and using the
    ActiveState build, at least) putting .pyc/.pyo in front of .py in the
    executable extension list -- meaning an older .pyc would be run instead
    of accessing the newer .py...

    When a module is run directly (not imported), no .pyc/.pyo files are
    generated. A module run directly gets the name "__main__" by the
    interpreter (an imported module gets the name used to import it). The

    if __name__ == "__main__":

    is a way for a module to be used as a standalone program, or test mode,
    by putting the test driver or standalone interface into the "if" block.
    When the module is imported, the "if" is false, so none of the code in
    it is executed.
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Dec 5, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    583
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    597
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    402
    Dan Sommers
    Jul 12, 2005
  4. Replies:
    1
    Views:
    504
    Sion Arrowsmith
    Jul 10, 2008
  5. Replies:
    20
    Views:
    6,705
Loading...

Share This Page