对象实例作为列表的成员->这是“ pythonic”吗?这是lege artis吗?

时间:2019-04-26 11:09:23

标签: python python-3.x oop design-patterns

我希望您对此代码有意见或建议

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


# Defines the class (Element) that will be used in the Container
class Element:
    def __init__(self, address):
        # The address is the identifier for this element
        self._address = int(address)

    def theaddress(self):
        # Getter for the address
        return self._address

    def __eq__(self, other):
        # This function will be called if we like to check if this instanse is already a
        # member of the container instance
        print("A __eq__ called: %r == %r ?" % (self, other))
        return self._address == other._address


# Defines the class (Container) which holds n instances of Element class
class Container:
    def __init__(self):
        # Basically it is a list
        self._listcontainers = list()

    # If we like to append a new element instance, we have to check if new one is already available
    # with the same address. So we like to never had the same address twice or more times!
    def appendcontainer(self, possibleElement: Element = Element(1)):
        # Calls the __eq__ element of the Element class to check if this one (with exactly this address) is already present
        if possibleElement in self._listcontainers:
            # If the possibleElement is in the container list
            print('Address %i of this element %s is already existing.' % (possibleElement.theaddress(), possibleElement))
            return False
        else:
            # If possobleElement is new append it to the containerlist
            self._listcontainers.append(possibleElement)
            print('Added element: %s with address: %i' % (possibleElement, possibleElement.theaddress()))
            return True

    # Returns the available elements in the containers
    def lengthcontainers(self):
        return len(self._listcontainers)


def main():
    # List of containers
    myContainer = Container()

    # New element with address 1
    newElement1 = Element(1)
    myContainer.appendcontainer(newElement1)

    # New element with address 2
    newElement2 = Element(2)
    myContainer.appendcontainer(newElement2)

    # New element with address xyz
    newElement3 = Element(2) # You can play with the addresses...
    myContainer.appendcontainer(newElement3)

    print('Available elements: %i' % myContainer.lengthcontainers())


if __name__ == "__main__":
    main()

现在:

  • 我有一个Element类,它表示某些数据的抽象。它本身也将包含一些逻辑作为私有方法...
  • 我有一个可容纳n个元素实例的类(在此示例中为容器)。
  • 我需要防止附加具有相同属性的元素。在此示例中-相同的地址。

问题:

  • 我想知道这是否是进行元素识别的最新技术?
  • 也许在Python 3中有更好的方法吗?
  • 特别是__eq__函数对我来说很有趣。因为_address应该是一个私有变量,并且我在other._address->“访问类的受保护成员_address ...”时收到警告。我需要在这里考虑些什么吗?

任何意见或建议对我都会很有帮助。其实我只是在做原型,但这应该是在这种情况下保存一些数据的真实项目的基础。

1 个答案:

答案 0 :(得分:1)

我认为魔术方法(如__eq____le__等)(根据代码样式)允许使用other对象的受保护成员,因此我将忽略警告。

至于附加,我建议尽可能使用set代替listset的工作是仅存储不同的元素,而您无需检查元素的存在,set会为您完成。不要忘记为您的__hash__类实现Element方法:

class Element:
    ...

    def __hash__(self):
        return hash(self._address)

    def __eq__(self, other):
        return self._address == other._address