Skip to content

Commit

Permalink
Implemented the Python attribute protocol. Now these work:
Browse files Browse the repository at this point in the history
hasattribute(element, 'aname')

getattribute(element, 'aname', [])
  • Loading branch information
apalala committed May 8, 2015
1 parent 06d60a8 commit c61743a
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion untangle.py
Expand Up @@ -80,7 +80,14 @@ def __getattr__(self, key):
self.__dict__[key] = matching_children
return matching_children
else:
raise IndexError('Unknown key <%s>' % key)
raise AttributeError(
"'%s' has no attribute '%s'" % (self._name, key)
)

def __hasattribute__(self, name):
if name in self.__dict__:
return True
return any(self.children, lambda x: x._name == name)

def __iter__(self):
yield self
Expand Down

0 comments on commit c61743a

Please sign in to comment.