19

I am going through the code of Python requests library from Kenneth Reitz (which is awesome!). And I have encountered a Class variable named __attrs__ (see below). Tried to find out something about it via Google and SymbolHound, but no luck.

Is this a standard Python thing? Where can I find more infos? Can someone enlighten me?

From: https://github.com/kennethreitz/requests/blob/master/requests/sessions.py

class Session(SessionRedirectMixin):
    ...

    __attrs__ = [
        'headers', 'cookies', 'auth', 'proxies', 'hooks', 'params', 'verify',
        'cert', 'prefetch', 'adapters', 'stream', 'trust_env',
        'max_redirects',
    ]

    def __init__(self):

        #: A case-insensitive dictionary of headers to be sent on each
        #: :class:`Request <Request>` sent from this
        #: :class:`Session <Session>`.
        self.headers = default_headers()
Ugur
  • 1,914
  • 2
  • 25
  • 46
  • It looks like they might have meant to use [`__slots__`](http://stackoverflow.com/questions/472000/usage-of-slots) – Ryan Haining Mar 15 '17 at 20:46
  • @RyanHaining: They set attributes not listed in `__attrs__`, though, such as `self.redirect_cache`. It looks like these are just the attributes they want to serialize in `__getstate__`. – user2357112 Mar 15 '17 at 20:51
  • 1
    this is only used for debugging (developer mode), aiohttp uses ATTRS for this – 0dminnimda Oct 13 '20 at 14:52

2 Answers2

29

This is not a standard Python thing. As far as I can tell, it's only there to be used in the __getstate__ method further down the class:

def __getstate__(self):
    state = dict((attr, getattr(self, attr, None)) for attr in self.__attrs__)
    state['redirect_cache'] = dict(self.redirect_cache)
    return state

The name __attrs__ is a poor choice, as names beginning and ending with __ are reserved for core Python language features.

user2357112
  • 260,549
  • 28
  • 431
  • 505
3

It is not standard, because it is not part of the Python Object Model.

The requests module uses it here (Response object), too, for reference

It just a way to keep some state hidden (at least as hidden as possible, because nothing is truly hidden in Python).

The writers of this code just delegate to this dictionary using a call that is part of the Python Object Model getattr:

return {attr: getattr(self, attr, None) for attr in self.__attrs__}
Jonathan Komar
  • 2,678
  • 4
  • 32
  • 43
  • It should really be `__attrs` then, without the trailing double underscore. Preceding **and** trailing dunders are by convention reserved for core Python features. – A Sz Mar 24 '23 at 09:23
  • @ASz Fine, but you can take your concerns to the writers of the requests module: [Open new Issue](https://github.com/psf/requests/issues/new/choose) – Jonathan Komar May 06 '23 at 09:00