Python - Как мне написать более эффективное Python-сокращение?

Я пытаюсь создать очень легкий класс Node, который будет служить инструментом поиска иерархии на основе Python. См. Определение ниже.

from functools import reduce
from operator import or_


class Node:

    def __init__(self, name):
        self.name = name
        self.children = []

    def add_child(self, child_node):
        self.children.append(child_node)

    def contains(self, other_node):
        if self == other_node:
            return True
        elif other_node in self.children:
            return True
        else:
            return reduce(or_, [child.contains(other_node)
                                for child in self.children], False)

    def is_contained_by(self, other_node):
        return other_node.contains(self)

    def __eq__(self, other_node):
        return self.name == other_node.name

    def __de__(self, other_node):
        return self.name != other_node.name

содержит , кажется, учебник по функциональному программированию (взят непосредственно из Почему функциональное программирование имеет значение ).

Вопрос: есть ли более эффективный или питонический способ письма содержит ? Я знаю, что map обычно заменяется пониманием списка, но я не видел лучшего способа сделать рекурсию на основе reduce .

Спасибо,

Mike

] === РЕДАКТИРОВАТЬ ... ЗДЕСЬ КЛАСС ПОВТОРЕНИЯ, УЧИТЫВАЯ ОТВЕТ И КОММЕНТАРИИ ===

class Node:

    def __init__(self, name):
        self.name = name
        self.children = []

    def add_child(self, child_node):
        # Hattip to lazyr for catching this.
        if self.contains(child_node) or child_node.contains(self):
            raise TreeError('A relationship is already defined.')    
        else:
            self.children.append(child_node)                

    def contains(self, other_node):
        # Hattip to lazyr for pointing out any() and to Jochen Ritzel for
        # eliminating the silly child check.
        return (self == other_node or
                any(child.contains(other_node) for child in self.children))

    def is_contained_by(self, other_node):
        return other_node.contains(self)

    def __eq__(self, other_node):
        return self.name == other_node.name

    def __de__(self, other_node):
        return self.name != other_node.name

    def __repr__(self):
        return self.name

10
задан MikeRand 18 March 2011 в 17:44
поделиться