我正在尝试将我的代码编译成Python 3模块.我在IDLE中选择"运行模块"时运行正常,但在尝试创建分发时收到以下语法错误:
File "/usr/local/lib/python3.2/dist-packages/simpletriple.py", line 9
def add(self, (sub, pred, obj)):
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助指出语法有什么问题吗?这是完整的代码:
import csv
class SimpleGraph:
def __init__(self):
self._spo = {}
self._pos = {}
self._osp = {}
def add(self, (sub, pred, obj)):
"""
Adds a triple to the graph.
"""
self._addToIndex(self._spo, sub, pred, obj)
self._addToIndex(self._pos, pred, obj, sub)
self._addToIndex(self._osp, obj, sub, pred)
def _addToIndex(self, index, a, b, c):
"""
Adds a triple to a specified index.
"""
if a not in index: index[a] = {b:set([c])}
else: …
Run Code Online (Sandbox Code Playgroud) 假设我想要一个元组列表.这是我的第一个想法:
li = []
li.append(3, 'three')
Run Code Online (Sandbox Code Playgroud)
结果如下:
Traceback (most recent call last):
File "./foo.py", line 12, in <module>
li.append('three', 3)
TypeError: append() takes exactly one argument (2 given)
Run Code Online (Sandbox Code Playgroud)
所以我求助于:
li = []
item = 3, 'three'
li.append(item)
Run Code Online (Sandbox Code Playgroud)
哪个有效,但看起来过于冗长.有没有更好的办法?