3

I am not really a python person, but having to deal with it just because a book author published their code examples in python. I get a syntax error for below code:

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)
...
...

error: def add(self, (sub, pred, obj)): ^ SyntaxError: invalid syntax

Is this a python version issue or something? I can't seem to be able to pass a tuple into a class method in that raw form. Suggestions appreciated. BTW fyi I just downloaded the Python 3.3 compiler.

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
  • 1
    See [PEP 3113](http://www.python.org/dev/peps/pep-3113/) for the discussion of why this syntax is no longer valid. – DSM Mar 05 '13 at 19:58
  • oh so it is an incorrect syntax for latest version of python. Let me try the older version, I only care if I can run these examples without much work and not actually interested in learning python itself. I will post my findings shortly – sridhar nallani Mar 05 '13 at 20:14

3 Answers3

4

Yes, it is a Python version issue. The tuple-argument-unpacking syntax is no longer allowed in Python 3. See http://www.python.org/dev/peps/pep-3113/ for explanation and examples of what to do instead.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Thanks all for sharing the answer so freakishly quickly... it did work with 2.7. Feels unfair to award the result to any one person but since this post has additional info of reasons why etc, I am selecting this as the answer, Thanks! – sridhar nallani Mar 05 '13 at 20:23
3

Try something like this:

def add(self, myTup):
    """
    Adds a triple to the graph.
    """
    (sub, pred, obj) = myTup
    self._addToIndex(self._spo, sub, pred, obj)
    self._addToIndex(self._pos, pred, obj, sub)
    self._addToIndex(self._osp, obj, sub, pred)
rlf_IV
  • 80
  • 7
  • mmm i can try that, but there are tons of examples and i wanted to avoid rewriting their code - kind of defeats my purpose as I will never use python for my purposes but just trying to understand the overall logic of things described. – sridhar nallani Mar 05 '13 at 20:11
  • so my question was, is the original code post supposed to error? was that not a right syntax - hard to imagine someone would publish a book without runnning them – sridhar nallani Mar 05 '13 at 20:12
  • Ah. Well, as others have stated, the original syntax is no longer valid. If you don't want to refactor the code then you could just download the Python 2.7 compiler and use that. – rlf_IV Mar 05 '13 at 20:13
  • >so my question was, is the original code post supposed to error? It will in Python 3.0+. – rlf_IV Mar 05 '13 at 20:14
1

That code is for python 2.7, but it's no more valid in version 3.0 and so on...

sissi_luaty
  • 2,839
  • 21
  • 28