示例 0:
>>> stus=['x','=',2,y] #不需声明,直接赋值
Traceback (most recent call last):
File "" , line 1, in <module>
stus=['x','=',2,y]
NameError: name 'y' is not defined
示例 1:
import datetime
last_id=0
class Note:
def __init__(self,memo,tags):
self.memo = memo
self.tags = tags
self.creation_date=datetime.date.today()
global last_id
last_id +=1
self.id=last_id
Shell中解释:
n1=Note(‘ddd’)
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘Note’ is not defined
表示在当前作用域中找不到名为 Xxx 的对象【变量】或函数
实际问题一般是类未定义,Python从模块中导入类的同时就代表在当前模块中找到定义了,所以有可能:
(1)未导入包含对应类声明的模块。
解决:需要在Shell先from notebook import Note
当然,还是产生错误:
from notebook import Note
n1=Note(‘dd’)
Traceback (most recent call last):
File “”, line 1, in
TypeError: init() missing 1 required positional argument: ‘tags’
示例 2:
模块mysubclass.py中代码:
class Contact:
all_contacts = []
def __init__(self, name, email):
self.name = name
self.email = email
class Supplier(Contact):
def order(self, order):
print("If this were a real system we would send ""'{}' order to '{}'".format(order, self.name))
上述代码‘F5’后,在Shell中解释:
1)单独导入子类:from mysubclass import Supplier
from mysubclass import Supplier
s=Supplier(‘’,‘’)
c=Contact(‘’,‘’)
Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘Contact’ is not defined
说明:仅导入子类不能defined父类,
*理论上,如果仅导入父类,必定也不能找到子类的定义
2)解决办法:from mysubclass import Supplier, Contact
或:from mysubclass import *
问题:感觉这种导入办法效率不高。
类型错误:init()方法()内缺少一个位置参数,需要(补全)positional argument: ‘tags’
情况:在子类函数中,采用supper()调用父类函数,被调用的函数中保留了参数‘self’,导致的错误
代码:
class BaseClass:
num_base_calls = 0
def call_me(self):
print("Calling method on Base Class")
self.num_base_calls += 1
class LeftSubclass(BaseClass):
num_left_calls = 0
def call_me(self):
super().call_me(self)
print("Calling method on Left Subclass")
self.num_left_calls += 1
class RightSubclass(BaseClass):
num_right_calls = 0
def call_me(self):
BaseClass.call_me(self)
print("Calling method on Right Subclass")
self.num_right_calls += 1
class Subclass(LeftSubclass, RightSubclass):
num_sub_calls = 0
def call_me(self):
LeftSubclass.call_me(self)
RightSubclass.call_me(self)
print("Calling method on Subclass")
self.num_sub_calls += 1
正确写法:
1)不采用super()
class LeftSubclass(BaseClass):
num_left_calls = 0
def call_me(self):
BaseClass.call_me(self) #采用父类名调用时,为啥需要self
print("Calling method on Left Subclass")
self.num_left_calls += 1
为啥一定要有self
如果没有,就会出现提示错误:
BaseClass.call_me()
TypeError: call_me() missing 1 required positional argument: ‘self’
2)采用supper()
错误写法:super().call_me(self)
正确写法:super().call_me()
否则就会出现下面的错误提示:
s.call_me()
Traceback (most recent call last):
File “”, line 1, in
File “F:\Traffic Property DeepLearning codes\mytest\multinh.py”, line 21, in call_me
LeftSubclass.call_me(self)
File “F:\Traffic Property DeepLearning codes\mytest\multinh.py”, line 9, in call_me
super().call_me(self)
TypeError: call_me() takes 1 positional argument but 2 were given
示例:
if self.slm.rowCount>0: #判断listview中的model的行数,用到是方法
无把方法当属性用了,即在方法后缺括号(),会提示该错误
示例代码:
```python
elif self.dm.sub_df.empty():
修改为:
```python
```python
elif self.dm.sub_df.empty: #sub_df是个dataframe
## IndentationError:
(1)意外缩进 unexpected indent
##
>>> from multinh import Subclass
>>> s=Subclass()
>>> s.call_me()
Traceback (most recent call last):
File "", line 1, in
File "F:\Traffic Property DeepLearning codes\mytest\multinh.py", line 21, in call_me
LeftSubclass.call_me(self)
File "F:\Traffic Property DeepLearning codes\mytest\multinh.py", line 9, in call_me
super().call_me(self)
TypeError: call_me() takes 1 positional argument but 2 were given
## ValueError:
### (1)ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
示例:
```python
elif self.dm.sub_df==None:
QMessageBox.information(self, "提示", "sub_df不存在,可能需要先“列切”")
else:
str=''
问题解析
这个错误是由于在if语句中尝试将Pandas的dataframe转换为bool类型值类判断。因为dataframe可以包含多个值,它们的布尔值不明确。
解决方法:if语句可改为:
elif self.dm.sub_df.empty: