Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If those nom's are unique keys, and all you are ever going to do is access your persons using this unique key you should indeed rather use a dictionary.</p> <p>However if you want to add more attributes over time and if you like to be able to retrieve one or more person by any of those attributes, you might want to go with a more complex solution:</p> <pre><code>class Example(): def __init__(self): self.__pList = [] def addPerson(self,name,number): self.__pList.append(Person(name,number)) def findPerson(self, **kwargs): return next(self.__iterPerson(**kwargs)) def allPersons(self, **kwargs): return list(self.__iterPerson(**kwargs)) def __iterPerson(self, **kwargs): return (person for person in self.__pList if person.match(**kwargs)) class Person(): def __init__(self,name,number): self.nom = name self.num = number def __repr__(self): return "Person('%s', %d)" % (self.nom, self.num) def match(self, **kwargs): return all(getattr(self, key) == val for (key, val) in kwargs.items()) </code></pre> <p>So let's assume we got one Mike and two Dave's</p> <pre><code>a = Example() a.addPerson('dave',123) a.addPerson('mike',345) a.addPerson('dave',678) </code></pre> <p>Now you can find persons by number:</p> <pre><code>&gt;&gt;&gt; a.findPerson(num=345) Person('mike', 345) </code></pre> <p>Or by name: </p> <pre><code>&gt;&gt;&gt; a.allPersons(nom='dave') [Person('dave', 123), Person('dave', 678)] </code></pre> <p>Or both:</p> <pre><code>&gt;&gt;&gt; a.findPerson(nom='dave', num=123) Person('dave', 123) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload