Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

384
Visualizações
Python __attrs__ explained

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()
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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__}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda