d0c-s4vage/pfp

Don't cause an exception in _pfp__children

Open

#32 opened on Mar 3, 2016

View on GitHub
 (1 comment) (0 reactions) (1 assignee)Python (37 forks)github user discovery
enhancementgood first issue

Repository metrics

Stars
 (203 stars)
PR merge metrics
 (PR metrics pending)

Description

I don't know if this would break something but wouldn't be better to have every element default to have an attribute _pfp__children equal to an empty list?

This way when write something like obj._pfp__children in say, traversing the DOM's graph (DFS or alike) you don't have to worry about exceptions and the code is more succinct.

Right now I am using an auxiliary function like:

def has_children(n):
    """ Returns boolean """
    try:
        n._pfp__children
        return True

    except AttributeError:
        return False

but it looks cumbersome and feels more natural to return an empty list to the question "give me a list containing your children"

Anyway, awesome work man :+1:

Contributor guide