Appending to and retrieving objects from Epiphany Py entities

This tutorial will get you started subclassing the ‘entities’ class in Epiphany Py to make array-like classes. Checkout version 1.1 to follow along.

First, let’s get some subclasses defined.

from entities import *
class gods(entities):

class god(entity):
    def __init__(self, name): = name

    def __str__(self):

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 gods collection.

norse_pantheon = gods()
assert norse_pantheon.count == 0

thor = god('Thor')
odin = god('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 thor and odin objects and append them to the norse_pantheon. Now the count property is 2.

The append method is only one way to add objects to our collection. Let’s clear the collection and try some other ways.

assert norse_pantheon.count == 0
assert norse_pantheon.isempty

norse_pantheon += thor
norse_pantheon += odin
assert norse_pantheon.count == 2

The clear method method empties the collection. Now we can assert that the count proprety is 0 and norse_pantheon.isempty is True.

Next we append thor and oden to the collection again, this time using the += operator.

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 += thor + odin
assert norse_pantheon.count == 2

Here, thor and odin are added together to make an entities collection which is then append to the norse_pantheon collection. Convenient, but for this to work, thor and odin had to inherit from the entity class.

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[0] is thor
assert norse_pantheon[1] 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 name value.

The third way is to use the first and second properties. (There are also third, fourth and 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:

Since we added the __str__ method to the god class, we can do this and it will do the same as the above.


Leave a Reply

Your email address will not be published. Required fields are marked *