Skip to main content

When to use __call__

Updated 1 min readMarkdown
When to use __call__

Requests

request.get(url, auth: AuthBase=obj, **kwargs)

accepts a obj: requests.auth.AuthBase to attach a user’s credentials required for a custom authentication flow.

It mutate's the request object to generate an “Authorization” Header using the context(content) of the request itself. It uses this hook before making the network calling obj(request)

Your class needs to be a callable which is done by implementing __call__(self, *args). This can be used as a general hook to support esoteric use cases based on type of the args your class is called with. It’s a meta programming equivalent of a class decorator as describe by this insightful pyconUS talk by Aditya Mehra.

References

About the author

  • I am developer/code-reviewer/debugger/bug-fixer/architect/teacher/builder from dubai, uae

No comments yet. Be the first to comment.