vendredi 26 avril 2019

Object instance as a member of a list --> Is this "pythonic"? Is this lege artis?

I like to have your opinion or a suggestion to this code:

#!/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()


Now:

  • I have a class Element which represents an abstraction of some data. It will contain some logic by itself as private methods too...
  • I have a class (Container in this example) which holds n element instances.
  • I need to prevent the appending of an element with the same properties. In this example - the same address.

Questions:

  • I like to know if this is state of the art for doing the identification of an element?
  • Maybe is there a better way for doing this in Python 3?
  • Especially the __eq__ function is very interesting for me. Because, _address should be a private variable and I get a warning at other._address --> "Access to protected member _address of a class...". Do I have to consider something here?

Any opinion or suggestion will be very helpful for me. Actually I'm doing only prototyping, but it should be the basic for an real project which holds some data in this case.

Aucun commentaire:

Enregistrer un commentaire