Prob. Code Downloaded for Programming the Semantic Web (python code)
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Jul 28 11:28:40 EDT 2014
On Mon, 28 Jul 2014 03:39:48 -0700, Bruce Whealton wrote:
> I setup Eclipse to use python 2.7.x and tried to run this and it just
> gave an error on line 9 where the def add function is declared.
First step is to confirm that Eclipse actually is using Python 2.7. Can
you get it to run this code instead? Put this in a module, and then run
it:
import sys
print(sys.version)
Once you have confirmed that Eclipse really is running 2.7, next is to
confirm what the syntax error actually is. Carefully check the source
code that there are no missing close-parentheses just before the "def
add" method. (Sometimes, a missing parenthesis won't show up as a Syntax
Error until *after* the actual error.)
It may be helpful to see the exact syntax error. If possible, cut and
paste (don't retype!) the full error message. It should look something
like this:
py> def add(a, (b, c)):
File "<stdin>", line 1
def add(a, (b, c)):
^
SyntaxError: invalid syntax
Notice that the caret ^ points to where Python discovers the error.
If you confirm that Eclipse is using Python 2.7, but it still complains
about the parenthesis, my guess -- and it's only a guess -- is that
somehow you have an invisible or non-printing character inserted into the
file at that position, and that's causing the Python parser to choke. I
can demonstrate a working example indirectly, with the exec function:
py> exec("def x(a,(b,c)): pass") # Works fine.
py> x
<function x at 0xb7e71a74>
Now here it is again, but with an invisible control character inserted:
py> exec("def x(a,\a(b,c)): pass")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
def x(a,(b,c)): pass
^
SyntaxError: invalid syntax
Notice that when Python prints the error, the \a control character
doesn't show up. It's there, but you can't see it.
> It just
> says invalid syntax and points at the parentheses that are in the
> function definition def add(self, (subj, pred, obj)):
> So, from what you said, and others, it seems like this should have
> worked but eclipse would not run it. I could try to load it into IDLE.
Whenever you have trouble with one IDE, it's good to get a second opinion
in another IDE. They might both be buggy, but they're unlikely to both
have the same bug.
Also, try to run the file directly from the shell, without an IDE. from
the system shell (cmd.exe if using Windows, bash or equivalent for
Linux), run:
python27 /path/to/yourfile.py
You'll obviously need to adjust the pathname, possibly even give the full
path to the Python executable.
[...]
>> In Python 3, that functionality was dropped and is no longer allowed.
>> Now you have to use the longer form.
>>
> I'm not sure I follow what the longer method is. Can you explain that
> more, please.
I referred to the parenthesised parameter version as a short cut for a
method that takes a single argument, then manually expands that argument
into three items. Let me show them together to make it more obvious:
# Unparenthesised version, with manual step.
def add(self, sub_pred_obj):
sub, pred, obj = sub_pred_obj
do_stuff_with(sub or pred or obj)
# Parenthesised shortcut.
def add(self, (sub, pred, obj)):
do_stuff_with(sub or pred or obj)
Both methods take a single argument, which must be a sequence of exactly
three values. The second version saves a single line, hence the first
version is longer :-)
--
Steven
More information about the Python-list
mailing list