nameerror

'NameError: global name is not defined' under pdb, for dictionary that does exist

一个人想着一个人 提交于 2019-12-03 16:09:32
I've encountered an issue re scopes in a lambda function. I can successfully output foo to stdout but I get an error when using max() including a lambda - see simplified code below... All in all, I am trying find the largest value for a nested key budget within an unknown number of first order keys. (Pdb) foo = self.some_method() # some_method() returns a dict, printed in the next step (Pdb) pp foo {'1': {'count': 1, 'extra_data': {'activity-count': 1, 'budget': 0, [...MORE KEY-VALUE PAIRS HERE...] 'version': 1}, [...LOTS MORE KEY-VALUE PAIRS HERE...] 'elements_total': defaultdict(<type 'int'>

NameError: global name 'long' is not defined

邮差的信 提交于 2019-12-03 05:14:01
I have a Python version 3.3.0 and I am not sure why it does not let me do long for b and m here... I tried to look up the answers on here and but nothing helped...thanks im getting an error saying NameError: global name 'long' is not defined power = long(b) % long(m) In Python 3.x, use int instead of long . From What’s New In Python 3.0, Integers : PEP 237 : Essentially, long renamed to int . That is, there is only one built-in integral type, named int ; but it behaves mostly like the old long type. 来源: https://stackoverflow.com/questions/14904814/nameerror-global-name-long-is-not-defined

Function name not defined

好久不见. 提交于 2019-12-02 18:48:26
问题 I have a pice of code which looks like this if __name__ == "__main__": main() def main(): print("hello") However, when I try to run this code I get the error NameError: name 'main' is not defined Have I not defined the name in the first line of the function "def main()"? 回答1: You should define main before call it def main(): print("hello") if __name__ == "__main__": main() 回答2: Have I not defined the name in the first line of the function "def main()"? Yes, but Python hasn't executed that

Why am I getting a NameError when I try to access an attribute in my class?

吃可爱长大的小学妹 提交于 2019-12-02 13:58:39
I have this code with a class: class Triangle(object): def __init__(self, side1, side2, side3): self.side1 = side1 self.side2 = side2 self.side3 = side3 def perimeter(self): return "Perimeter = %s" % (side1 + side2 + side3) a = Triangle(3, 4, 5) print(a.perimeter()) Running this code throws an exception: Traceback (most recent call last): File "untitled.py", line 12, in <module> print(a.perimeter()) File "untitled.py", line 9, in perimeter return "Perimeter = %s" % (side1 + side2 + side3) NameError: name 'side1' is not defined How come I can't access side1 in the perimeter method? Replace

Python NameError from contents of a variable

百般思念 提交于 2019-12-02 07:25:59
I've been making a mod for the Raspberry Pi version of Minecraft and I'm getting a very frustrating error every time I enter one of the commands in my program. Here's my code: import minecraft.minecraft as minecraft import minecraft.block as block import time mc = minecraft.Minecraft.create(); print('newBlock - Change ID of block to spawn') print('blockType - Change subID of block to spawn') print('pos1') print('pos2') print('fill - fill specified area') print('clear - clear specified area') print while True: comm=str(input('Command: ')) if comm=="newBlock": blockId = int(input('Enter Block ID

Getting the name which is not defined from NameError in python

梦想的初衷 提交于 2019-12-01 16:22:47
As you know, if we simply do: >>> a > 0 Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> a > 0 NameError: name 'a' is not defined Is there a way of catching the exception/error and extracting from it the value 'a'. I need this because I'm eval uating some dynamically created expressions, and would like to retrieve the names which are not defined in them. Hope I made myself clear. Thanks! Manuel >>> import re >>> try: ... a>0 ... except (NameError,),e: ... print re.findall("name '(\w+)' is not defined",str(e))[0] a If you don't want to use regex, you could do something

Getting the name which is not defined from NameError in python

被刻印的时光 ゝ 提交于 2019-12-01 15:12:50
问题 As you know, if we simply do: >>> a > 0 Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> a > 0 NameError: name 'a' is not defined Is there a way of catching the exception/error and extracting from it the value 'a'. I need this because I'm eval uating some dynamically created expressions, and would like to retrieve the names which are not defined in them. Hope I made myself clear. Thanks! Manuel 回答1: >>> import re >>> try: ... a>0 ... except (NameError,),e: ... print

Python NameError (def) is not defined [duplicate]

ぐ巨炮叔叔 提交于 2019-12-01 09:03:34
This question already has an answer here: Python call function within class 2 answers I'm having trouble with the following Python code: class Methods: def method1(n): #method1 code def method2(N): #some method2 code for number in method1(1): #more method2 code def main(): m = Methods for number in m.method2(4): #conditional code goes here if __name__ == '__main__': main() When I run this code, I get NameError: name 'method1' is not defined. How do I resolve this error? Just add self. in front of it: self.method1(1) Also change your method signitures to: def method1(self, n): and def method2

Why is my python function not defined, when it exists in the same file?

若如初见. 提交于 2019-11-29 14:25:56
I have a simple function, which I shall call myFunction . It takes two parameters, performs some calculations on them, and returns the result. I also have a class, MyClass , which has a constructor that has a header like this: __init__(self, bar, fun=myFunction): When I try to run anything in this class, I get the following error: MyClass def __init__(self, bar, fun=myFunction): NameError: name 'myFunction' is not defined If I remove this class, I can use myFun in the Python Shell, so what's the deal? You haven't shown the actual code so it's hard to be sure, but I bet myFunction is defined

Python tkinter 8.5 import messagebox

二次信任 提交于 2019-11-29 12:05:29
The following code runs fine within IDLE, but otherwise I get "NameError: global name 'messagebox' is not defined". However, if I explicitly state from tkinter import messagebox , it runs fine from where ever. from tkinter import * from tkinter import ttk root = Tk() mainFrame = ttk.Frame(root) messagebox.showinfo("My title", "My message", icon="warning", parent=mainFrame) Why does IDLE not need the explicit import statement but elsewhere it is required? the messagebox is a separate submodule of tkinter, so simply doing a complete import from tkinter: from tkinter import * doesn't import