Skip to content

Commit bd8a39e

Browse files
authored
Merge pull request #727 from xgi-org/docs-edges-getitem
docs: clarify that H.edges[idx] returns attributes, not members
2 parents e2ccb1f + 2c92ffb commit bd8a39e

1 file changed

Lines changed: 32 additions & 6 deletions

File tree

xgi/core/views.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,43 @@ def __iter__(self):
143143
return iter(self._ids)
144144

145145
def __getitem__(self, idx):
146-
"""Get the attributes of the ID.
146+
"""Get the attribute dictionary of the node or edge with the given ID.
147+
148+
Note that this returns the **attributes** (a dict of metadata), not the
149+
members of an edge or the edges containing a node. If no attributes have
150+
been set, the returned dict is empty.
147151
148152
Parameters
149153
----------
150154
idx : hashable
151-
node or edge ID
155+
Node or edge ID.
152156
153157
Returns
154158
-------
155159
dict
156-
attributes associated to the ID.
160+
Attributes associated with `idx`. Empty if none have been set.
157161
158162
Raises
159163
------
160-
XGIError
161-
If the id is not being kept track of by this view, or if id is not in the
162-
hypergraph, or if id is not hashable.
164+
IDNotFound
165+
If `idx` is not in this view.
166+
167+
See Also
168+
--------
169+
:meth:`EdgeView.members` : Get the nodes that make up an edge.
170+
:meth:`NodeView.memberships` : Get the edges containing a node.
171+
172+
Examples
173+
--------
174+
>>> import xgi
175+
>>> H = xgi.Hypergraph([[1, 2, 3], [3, 4]])
176+
>>> H.edges[0] # attribute dict (empty by default)
177+
{}
178+
>>> H.edges.members(0) # the actual members of edge 0
179+
{1, 2, 3}
180+
>>> H.add_edge([5, 6], idx="e1", color="red")
181+
>>> H.edges["e1"] # attributes set at creation
182+
{'color': 'red'}
163183
164184
"""
165185
if idx not in self:
@@ -576,6 +596,9 @@ class NodeView(IDView):
576596
`tutorial
577597
<https://xgi.readthedocs.io/en/stable/api/tutorials/focus_6.html>`_.
578598
599+
Indexing with ``H.nodes[id]`` returns the node's **attribute dictionary**,
600+
not the edges it belongs to. Use :meth:`memberships` for the latter.
601+
579602
"""
580603

581604
_id_kind = "node"
@@ -688,6 +711,9 @@ class EdgeView(IDView):
688711
`tutorial
689712
<https://xgi.readthedocs.io/en/stable/api/tutorials/focus_6.html>`_.
690713
714+
Indexing with ``H.edges[id]`` returns the edge's **attribute dictionary**,
715+
not its members. Use :meth:`members` for the latter.
716+
691717
"""
692718

693719
_id_kind = "edge"

0 commit comments

Comments
 (0)