Reputation: 695
def get_connection_tag(connections_tag, connection_type):
for child in connections_tag.children:
concat = "".join(child.attributes)
if connection_type in concat:
return child
return -1
This function behaves as a search function to find if the specified connection_type
exists within the connection_tag
. If it is successful it returns an element from connections_tag.children
, but if unsuccessful it returns -1.
If the search function was successful I want to call a function to modify this child element, but if it is unsuccessful, I want to call a function to generate a child element.
I could simply call isinstance()
with the returned child and Class, or I could check to see if the returned child == -1
, but I feel like I'm missing a more appropriate way. Perhaps something to do with try/except and raising a TypeError
?
Upvotes: 1
Views: 177
Reputation: 1474
In this case returning "None" would be more appropriate as it intuitively indicates that no connection was found. Alternatively you could also raise an exception, I usually raise an exception if the execution has to stop or it's a more serious error.
Based on your use case the code would look like this:
tag = get_connection_tag(connections_tag, connection_type)
if not tag:
pass
Upvotes: 2