Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
What's the point of using [object instance].__self__?

I was checking the code of the toolz library's groupby function in Python and I found this:

def groupby(key, seq):
    """ Group a collection by a key function
    """
    if not callable(key):
        key = getter(key)
    d = collections.defaultdict(lambda: [].append)
    for item in seq:
        d[key(item)](item)
    rv = {}
    for k, v in d.items():
        rv[k] = v.__self__
    return rv

Is there any reason to use rv[k] = v.__self__ instead of rv[k] = v?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This is a somewhat confusing trick to save a small amount of time:

We are creating a defaultdict with a factory function that returns a bound append method of a new list instance with [].append. Then we can just do d[key(item)](item) instead of d[key(item)].append(item) like we would have if we create a defaultdict that contains lists. If we don't lookup append everytime, we gain a small amount of time.

But now the dict contains bound methods instead of the lists, so we have to get the original list instance back via __self__.

__self__ is an attribute described for instance methods that returns the original instance. You can verify that with this for example:

>>> a = []
>>> a.append.__self__ is a
True
over 4 years ago · Santiago Trujillo Report

0

This is a somewhat convoluted, but possibly more efficient approach to creating and using a defaultdict of lists.

First, remember that the default item is lambda: [].append. This means create a new list, and store a bound append method in the dictionary. This saves you a method bind on every further append to the same key, and the garbage collect that follows. For example, the following more standard approach is less efficient:

d = collections.defaultdict(list)
for item in seq:
    d[key(item)].append(item)

The problem then becomes how to get the original lists back out of the dictionary, since the reference is not stored explicitly. Luckily, bound methods have a __self__ attribute which does just that. Here, [].append.__self__ is a reference to the original [].

As a side note, the last loop could be a comprehension:

return {k: v.__self__ for k, v in d.items()}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!