473,574 Members | 3,125 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

global name 'self' is not defined

Hi

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

import canPlaces as canp

callOne=canp.ad dMe(3,5)
callTwo=canp.es tocStn()

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

Dec 2 '06 #1
8 10529
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.ad dMe(3,5)
callTwo=canp.es tocStn()

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

Dec 2 '06 #2
On 2 Dec 2006 10:42:28 -0800, Evan <ev*******@gmai l.comwrote:
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.
Dec 2 '06 #3
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

Dec 2 '06 #4

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>\python 25\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

Dec 2 '06 #5
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.

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

import canaryPlaces_te st as canp

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

The problem script is this one, named canaryPlaces_te st.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/mappingdiff canaryPlaces_te st.py canaryPlaces.py
1c1
<
---
# canaryPlaces
evan@suse212:~/python/mapping>
>From the command line I get what I expect calling either
canaryPlaces_te st.py or canaryPlaces.py :

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

However -Using ipython and calling canaryPlaces.py is fine, but
calling canaryPlaces_te st.py gives:

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

/home/evan/python/mapping/call.py
2
3 sum=canp.addme( 3,5)
4 print sum
----5 estoc=canp.esto cStn()
6 print estoc
/home/evan/python/mapping/canaryPlaces_te st.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.
Dec 4 '06 #7
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

Dec 5 '06 #8
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>

Dec 5 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1479
by: Tian | last post by:
I want to create a object directory called Context in my program, which is based on a dict to save and retrieve values/objects by string-type name. I have the definition like this: utils.py -------------------- global sysctx class Context: def __init__(self):
59
3895
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a candidate for Python 3000 yet? Chris
3
1475
by: wanwan | last post by:
I'm trying to make a GUI, but for some of the instantiated object variable names, the interpreter is looking at them as global names. Here is an example of what I did: class mygui: def __init__(self, root):
5
6698
by: NetKev | last post by:
I added a function 'warn_Admin' and defined it just before another function 'process_log'. 'process_log' calls this warn_Admin' function. However, when it gets called i get the following error every time: --- Traceback (most recent call last): File "/usr/bin/denyhosts.py", line 202, in ? first_time, noemail, daemon) File...
1
12454
by: NachosRancheros | last post by:
Ok so I just started to program with Python about a week ago and I am trying to make a program that will take the path of a file and a shortcut command and save it to a text file. Eventually I want to have a little command line box that sits on the desktop that I can execute those commands from but that is for another day. At the moment I am just...
1
29326
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this...
2
1650
by: RgeeK | last post by:
I'm seeing something which make me think I'm missing something about how global var's behave. I've defined a global string, right at the start of my .py file. outXMLfile = "abc" I define a class and do a bunch of stuff below that. Then I have another class, and in it, there is a method 'def' that has: def OnOutfileButton(self,evt):
3
18019
by: Netwatcher | last post by:
im try to activate the tutorial code for wxPython, it is working properly when i tell it to import * from wxPython.wx but with an error about the package, i've done as i was told to change the import to wx and change all wxsomething to wx.something but when i start it it gives me an error that say Traceback (most recent call last): File...
4
4245
by: jorgejch | last post by:
Hello, I've started with python (3) recently. Initialy only for scripting. Now I'm trying the object oriented bit. I'm getting the following error message <Atom.Atom object at 0x7f0b09597fd0> Traceback (most recent call last): File "./Main.py", line 7, in <module> print (t.getAtomName()) File...
0
7805
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8234
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7815
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5300
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3740
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2243
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.