How To Implement A Secondary Custom Method For Object Slicing, Other Than __getitem__ In Python

- 1 answer

I am looking to implement a custom method in my class which helps users slice based on index. The primary slicing will be based on dictionary key. I want to implement it similar to how Pandas does it, using df.iloc[n]

here's my code:

class Vector:
    def __init__(self, map_object: dict):
        self.dictionary = map_object
    def __getitem__(self, key):
        data = self.dictionary[key]
        return data

    def iloc(self, n):
        key = list(self.dictionary)[n]
        return self.dictionary[key]

However, if then write object.iloc[3] after creating the object, I get an error saying 'method' object is not subscriptable. So how can I implement this?



The [] syntax requires a proper object with a __getitem__ method. In order to have a "slice method", use a property that returns a helper which supports slicing.

The helper simply holds a reference to the actual parent object, and defines a __getitem__ with the desired behaviour:

class VectorIloc:
    def __init__(self, parent):
        self.parent = parent

    # custom logic for desired "iloc" behaviour
    def __getitem__(self, item):
        key = list(self.parent.dictionary)[item]
        return self.parent[key]

On the actual class, merely define the desired "method" as a property that returns the helper or as an attribute:

class Vector:
    def __init__(self, map_object: dict):
        self.dictionary = map_object
        # if .iloc is used often
        # self.iloc = VectorIloc(self)

    def __getitem__(self, key):
        return self.dictionary[key]

    # if .iloc is used rarely
    def iloc(self):
        return VectorIloc(self)

Whether to use a property or an attribute is an optimisation that trades memory for performance: an attribute constructs and stores the helper always, while a property constructs it only on-demand but on each access. A functools.cached_property can be used as a middle-ground, creating the attribute on first access.
The property is advantageous when the helper is used rarely per object, and especially if it often is not used at all.

Now, when calling vector.iloc[3], the vector.iloc part provides the helper and the [3] part invoces the helper's __getitem__.

>>> vector = Vector({0:0, 1: 1, 2: 2, "three": 3})
>>> vector.iloc[3]