First, let’s get some subclasses defined.
from entities import * class gods(entities): pass class god(entity): def __init__(self, name): self.name = name def __str__(self): return self.name
Here, our array-like class is
gods since it inherits from
entities. The second class,
god inherits from
entity. Instances of this class will will be elements of the
gods. It doesn’t have to inherit from
entity but it helps and one operations is impossible unless it does (we’ll see which one below).
Let’s create some gods and append them to the
norse_pantheon = gods() assert norse_pantheon.count == 0 thor = god('Thor') odin = god('Odin') norse_pantheon.append(thor) norse_pantheon.append(odin) assert norse_pantheon.count == 2
After instantiating our
norse_pantheon collection, we can assert that its
count property is 0 since we haven’t added any elements yet. Next we create
odin objects and append them to the
norse_pantheon. Now the
count property is 2.
append method is only one way to add objects to our collection. Let’s clear the collection and try some other ways.
norse_pantheon.clear() assert norse_pantheon.count == 0 assert norse_pantheon.isempty norse_pantheon += thor norse_pantheon += odin assert norse_pantheon.count == 2
clear method method empties the collection. Now we can assert that the
count proprety is 0 and
Next we append
oden to the collection again, this time using the
append method and the
+= operator do the same thing here. I prefer
+= since it reduces the number of characters on the line. Once you get used to it, it’s easier to read.
There is one more method to append object to
entities collection which is very useful in some circumstances.
norse_pantheon.clear() norse_pantheon += thor + odin assert norse_pantheon.count == 2
odin are added together to make an
entities collection which is then append to the norse_pantheon collection. Convenient, but for this to work,
odin had to inherit from the
So far, we’ve added to the collection. Now, let’s explore ways to get the objects out of the collection. Here are three ways to do the same thing.
assert norse_pantheon is thor assert norse_pantheon is odin assert norse_pantheon['Thor'] is thor assert norse_pantheon['Odin'] is odin assert norse_pantheon.first is thor assert norse_pantheon.second is odin
The first uses the collection indexer to to get the object out by it’s index number. The first object added to a collection will be indexed at 0, the second will be index at 1 and so on.
The second way is to use the
name property as the index. Since we instantiated the
god objects so their name properties would be ‘Thor’ and ‘Odin’ (see the constructor), we can obtain them by their
The third way is to use the
second properties. (There are also
fifth properties. At the moment, there is no
onehundredandseventyseventh property but I am looking in to a way to do that)
We can also iterate over the collection.
for g in norse_pantheon: print(g.name)
Since we added the
__str__ method to the
god class, we can do this and it will do the same as the above.