caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
812 stars 186 forks source link

There is no need to convert `key_func` from `None` into an identity function. #93

Open GalacticStarfish opened 6 years ago

GalacticStarfish commented 6 years ago

Inside tree.__print_backend you'll find:

   # legacy ordering
    if key_func is None:
        def key_func(node):
            return node

Presumably, this was done for the call which appears later in __get_iter:

children.sort(key=key_func, reverse=reverse)'

However, is perfectly okay to pass key=None into list.sort. In fact, None is the default value for sort, and list.sort already replaces key with an identity function in such cases. The following works just fine:

children = ['Fred', 'Sarah', 'Ian', 'Joe', 'William', 'Abigale']
children.sort(key=None) # modifies in-place. returns `None`
print(children)