Graph.__contains__

18 views
Skip to first unread message

g.zyc...@gmail.com

unread,
Oct 30, 2015, 7:21:51 AM10/30/15
to rdflib-dev
Hi all,

I have a question regarding implementation of two __contains__ methods in rdflib/graph.py (sorry for bad formatting if any):

def __contains__(self, triple):
"""Support for 'triple in graph' syntax"""
for triple in self.triples(triple):
return True
return False

def __contains__(self, triple_or_quad):
"""Support for 'triple/quad in graph' syntax"""
s,p,o,c = self._spoc(triple_or_quad)
for t in self.triples((s,p,o), context=c):
return True
return False

These seem odd to me in that they return True immediately inside the loop without any conditionals present. According to my (limited) understanding of Python, the loops shall end unconditionally here after first pass.

What is the motivation behind this? Can someone explain in more detail how this actually works?

Thanks,
Grzegorz

Jörn Hees

unread,
Oct 30, 2015, 8:40:24 AM10/30/15
to rdfli...@googlegroups.com

> On 30 Oct 2015, at 12:21, g.zyc...@gmail.com wrote:
> These seem odd to me in that they return True immediately inside the loop without any conditionals present. According to my (limited) understanding of Python, the loops shall end unconditionally here after first pass.
>
> What is the motivation behind this? Can someone explain in more detail how this actually works?

the loops only loop over the triple / quad in question, so they only ever loop once if it is contained in the graph.

see the first loop for example and have a look at the graph's triples method:
> for triple in self.triples(triple):


Best,
Jörn

Reply all
Reply to author
Forward
0 new messages