python - Python:为什么会出现“意外的关键字参数”错误?
问题描述
我正在尝试设置图表。对于初始化,我想要选择是否从节点和边的集合开始。所以我给了他们默认值None。或者我是这么想的:
def Graph():
def __init__(self, nodes=None, edges=None, msg="test"):
"""
assumes that the node and edge lists are the respective objects
"""
if nodes == None:
self.nodes = []
else:
self.nodes = nodes
if edges == None:
self.edges = []
else:
self.edges = edges
self.node_names = []
for node in nodes:
self.node_names.append(node.get_name())
self.msg = msg
(味精部分是用最简单的例子测试代码)
我得到的是:
g = Graph(msg="33")
Traceback (most recent call last):
File "<ipython-input-29-cc459c9baef3>", line 1, in <module>
g = Graph(msg="33")
TypeError: Graph() got an unexpected keyword argument 'msg'
有谁能够帮我?这可能是一件简单得可笑的事情,但我只是没有看到它,而且我在这里有点生气......
解决方案
您没有将其定义Graph
为类,而是定义为常规函数。
替换def Graph():
为class Graph:
。
推荐阅读
- reactjs - React Native:如何使用 TouchableOpacity onPress 更改 BackgroundColor
- twilio - Twilio Video 手动开始录制
- kotlin - 带有安全运算符的 Kotlin 非空断言
- c++ - C++ 递归地遍历路径。错误:未找到采用“const std::filesystem::directory_entry”类型的右手操作数的运算符
- python - 如何在 web2py 中呈现 PyChart.JS 图表?
- ios - GCD串行异步队列与嵌套在异步中的串行同步队列
- python - 如何在 Python 2 中使用 glob() 递归查找路径?
- javascript - Touchstart 和 Touchend 在移动设备上不起作用
- kubernetes - 如何在 kubernetes Dashboard 上将用户限制为一个命名空间?
- python - VS Code Python 脚本第二天突然停止工作