Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to implement a secondary custom method for object slicing, other than __getitem__ in Python

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
    @property
    def iloc(self):
        return VectorIloc(self)

Whether to use a property or an attribute is an optimisation that trades memory for performance. The property is advantageous when the helper is used rarely.


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]
3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading