nameerror

NameError: name 'self' is not defined

独自空忆成欢 提交于 2019-12-17 08:09:43
问题 Why such structure class A: def __init__(self, a): self.a = a def p(self, b=self.a): print b gives an error NameError: name 'self' is not defined ? 回答1: Default argument values are evaluated at function define-time, but self is an argument only available at function call time. Thus arguments in the argument list cannot refer each other. It's a common pattern to default an argument to None and add a test for that in code: def p(self, b=None): if b is None: b = self.a print b 回答2: For cases

Python: NameError: global name 'foobar' is not defined [duplicate]

天涯浪子 提交于 2019-12-17 03:41:40
问题 This question already has answers here : Python call function within class (2 answers) Closed 7 months ago . I have written the following class: class myClass(object): def __init__(self): pass def foo(self, arg1, arg2): pp = foobar(self, arg1, arg2) if pp: return 42 else return -666 def foobar(self, arg1, arg2): if arg1 == arg2: return 42 else: return None The logic is nonsensical - ignore it. What I am trying to so is to call an instance method from another instance method - and I am getting

Python NameError: name is not defined

牧云@^-^@ 提交于 2019-12-17 02:31:39
问题 I have a python script and I am receiving the following error: Traceback (most recent call last): File "C:\Users\Tim\Desktop\pop-erp\test.py", line 1, in <module> s = Something() NameError: name 'Something' is not defined Here is the code that causes the problem: s = Something() s.out() class Something: def out(): print("it works") This is being run with Python 3.3.0 under Windows 7 x86-64. Why can't the Something class be found? 回答1: Define the class before you use it: class Something: def

Python NameError: name is not defined

放肆的年华 提交于 2019-12-17 02:30:22
问题 I have a python script and I am receiving the following error: Traceback (most recent call last): File "C:\Users\Tim\Desktop\pop-erp\test.py", line 1, in <module> s = Something() NameError: name 'Something' is not defined Here is the code that causes the problem: s = Something() s.out() class Something: def out(): print("it works") This is being run with Python 3.3.0 under Windows 7 x86-64. Why can't the Something class be found? 回答1: Define the class before you use it: class Something: def

Ipython notebook : Name error for Imported script function

China☆狼群 提交于 2019-12-14 03:27:03
问题 I have two scripts sources.py and nest.py . They are something like this sources.py import numpy as np from nest import * def make_source(): #rest of the code def detect(): Nest = nest() Nest.fit() if __name__=='main': detect() nest.py import numpy as np from sources import * class nest(object): def _init_(self): self.source = make_source() def fit(self): #rest of the code When I run the script like python sources.py It works fine. But in the Ipython notebook environment if I do the following

Getting a name error when trying to input a string [duplicate]

拜拜、爱过 提交于 2019-12-13 15:42:17
问题 This question already has answers here : error in python d not defined. [duplicate] (3 answers) Closed 5 years ago . import pickle import os import time class Person(): def __init__(self, number, address): self.number = number self.address = address def save(): with open('mydict.pickle', 'wb') as f: pickle.dump(mydict, f) mydict = {} mydict['Avi'] = ['347-000-0000', 'Oceanview'] mydict['Alan'] = ['347-000-0000', 'Brighton'] mydict['Frank'] = ['718-000-0000', 'Brighton'] print('add a name to

Python - “NameError: name 'filename' is not defined”

霸气de小男生 提交于 2019-12-13 11:14:59
问题 Build mongoimport command collection = filename[:filename.find('.')] working_directory = 'C:/Users/Anshuman Misra/Downloads/' json_file = filename + '.json' mongoimport_cmd = 'mongoimport -h 127.0.0.1:27017 ' + \ '--db ' + db_name + \ ' --collection ' + collection + \ ' --file ' + working_directory + json_file Before importing, drop collection if it exists (i.e. a re-run) if collection in db.collection_names(): print 'Dropping collection: ' + collection db[collection].drop() Execute the

WinPython: NameError: name 'time' is not defined only inside a function

拥有回忆 提交于 2019-12-13 03:29:56
问题 I'm using WinPython to run my python program using a pendrive. But when i try to run an imported function inside a function i created, i receave the following error: NameError: name 'time' is not defined (it happens with any imported module) Here an example that works fine: import time time.sleep(0.5) Here an exemple that doesn't work: import time def waitLoad(): time.sleep(0.2) waitLoad() I receive the following error executing the example above: Traceback (most recent call last): File "E:

Python import module results in NameError

天涯浪子 提交于 2019-12-13 02:37:38
问题 I'm having a module import issue. using python 2.6 on ubuntu 10.10 I have a class that subclasses the daemon at http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/ . I created a python package with a module containing code that imports some models from a django project. The code works when used from a class, not subclassing the daemon. The structure looks something like: my_module __init__.py - bin - cfg - core __init__.py collection.py daemon.py The ItemRepository

Functions NameError

一世执手 提交于 2019-12-13 01:16:38
问题 I have been messing around with some code, trying to create a function for work planning. However I am stuck and wondered if someone could help? Thanks class Work_plan(object): def __init__(self,hours_work,work_len, work_des): self.hours_work = hours_work self.work_len = work_len self.work_des = work_des work_load = [] hours_worked = [] if hours_worked > hours_work: print "Too much work!" else: work_load.append(work_des) hours_worked.append(work_len) print "The work has been added to your