Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda