Prob. Code Downloaded for Programming the Semantic Web (python code)

Bruce Whealton futurewavewebdevelopment at gmail.com
Mon Jul 28 12:39:48 CEST 2014


On Friday, July 25, 2014 9:28:32 PM UTC-4, Steven D'Aprano wrote:
> On Fri, 25 Jul 2014 17:06:17 -0700, Bruce Whealton wrote:
Steven,
See below please.  The explanation did help.   
> 
> > OK, Eclipse with PyDev doesn't like this first line, with the function:
> 
> > def add(self, (sub, pred, obj)):
> 
> 
> 
> In Python 2, you could include parenthesised parameters inside function
> declarations as above. That is effectively a short cut for this version,
> where you collect a single argument and then expand it into three
> variables:
> 
> 
> 
>     def add(self, sub_pred_obj):
> 
>         sub, pred, obj = sub_pred_obj
> 
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.  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.
> 
> 
> 
> 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. 
> 
> 
> [...]
> 
> > There are other places where I thought that there were too many
> 
> > parentheses and I tried removing one set of them.  For example this
> 
> > snippet here:
> 
> > 
> 
> >     def remove(self, (sub, pred, obj)):
> 
> >         """
> 
> >         Remove a triple pattern from the graph. """
> 
> >         triples = list(self.triples((sub, pred, obj)))
> 
> 
> 
> Firstly, the remove method expects to take a *single* argument (remember
> 
> that self is automatically provided by Python) which is then automatically
> 
> expanded into three variables sub, pred, obj. So you have to call it with a
> 
> list or tuple of three items (or even a string of length exactly 3).
> 

> Then, having split this list or tuple into three items, it then joins them
> 
> back again into a tuple:
> 
> 
> 
>     (sub, pred, obj)
 
> passes that tuple to the triples method:
 
>     self.triples((sub, pred, obj))
> 
> 
> 
> (not shown, but presumably it uses the same parenthesised parameter trick),
> 
> and then converts whatever triples returns into a list:
> 
The full code listing should be available in the code paste link that I included.
> 
> 
>     list(self.triples((sub, pred, obj)))
> 
> 
> 
> that list then being bound to the name "triples".
> 
> 
Thanks, the explanation helped,
Bruce



More information about the Python-list mailing list