Newbie help - Programming the Semantic Web with Python

Bruce Whealton bruce at whealton.info
Mon Jul 11 18:04:18 EDT 2011


This didn't seem to work either.  I was getting errors the number of 
arguments expected being two, when I changed to
def add (self, args):
I seem to remember that if I removed the parentheses around sub, pred, obj, 
it worked.  I thought that was how it worked.  What is strange is that this 
is not reported as an error on the books page.  So, it should have worked as 
is with either Python 2.7, which I have installed or Python 3.0 which I also 
have installed.  So, it seems like it would have worked as is for one of the 
versions of Python, but it doesn't seem to work that way.
I'll paste a link to where the code exists.  Could someone help me figure it 
out please.  The code is here on the site:
http://semprog.com/content/the-book/

I wonder if I can also try it out from the IDLE interactive session.
Thanks,
Bruce

On Sun, Jul 10, 2011 at 11:32 AM, Bruce Whealton <bruce at whealton.info> 
wrote:
problem with is this line:
>     def add(self, (sub, pred, obj)):
> I think the problem is with the parentheses before the sub.  I removed 
> those and that seemed to fix that error or make it go away.  I don’t 
> remember how I figured that out,   It should be on the Errata page for 
> sure.
> Then it has a problem with this line:
>     print list(g.triples((None, None, None)))
> If I was using python 3, it would require () around the thing that is 
> going to be printed, right?  Maybe python 2.7 doesn’t like this line for 
> the same reason.
>

The issue there is with tuple unpacking. To match the older syntax,
don't touch the call, but change the definition thus:
def add(self, args):
  (sub, pred, obj)=args

Or, of course, simply list the arguments directly, rather than in a
tuple; but that requires changing every call (if it's a small program
that may not be a problem).

You're right about needing parentheses around the print() call; in
Python 2 it's a statement, but in Python 3, print is a function like
any other.

Regarding the module search path, this may help:
http://docs.python.org/dev/tutorial/modules.html#the-module-search-path

Chris Angelico
-- 
http://mail.python.org/mailman/listinfo/python-list 




More information about the Python-list mailing list