Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8326425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:45:26+00:00 2026-06-09T00:45:26+00:00

The Twython module uses requests module internally. I want to wrap/decorate request’s requests.post(*k, **kw)

  • 0

The Twython module uses requests module internally.

I want to wrap/decorate request’s requests.post(*k, **kw) method so everything Twython makes a request.post(...) call it will be transparently decorated/wrapped without interfering with the Twython module.

If I edited the requests codebase that’d be easy enough but I’m curious how to solve the general problem of adding a decorator to an already defined function/method.

import requests
def magic_wrapper_doodad(...)
...
...
requests.post = magic_wrapper_doodad(my_function, requests.post) # plz?

import Twython

# thanks to the above magic, requests.post is wrapped just as if it was defined like:
@decorator
def trace(f, *args, **kw):
    print("calling %s with args %s, %s" % (f.__name__, args, kw))
    return f(*args, **kw)

...
... #inside requests.py now:
@trace
def post(self, *args, **kw):
...

how do I write magic_wrapper_doodad() – or some alternative code – so I can decorate the code like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:45:28+00:00Added an answer on June 9, 2026 at 12:45 am

    Decorator @ notation is just syntactic sugar for calling the decorator callable with the decorated function and replacing the decorated function with the result.

    In other words, the following two examples are the exact same thing:

    @yourdecorator
    def a_function():
        pass
    
    def b_function():
        pass
    b_function = yourdecorator(b_function)
    

    and the same applies to methods in class definitions.

    Thus, you can simply decorate a class method by replacing the class method with the result of the decorator callable:

    requests.post = my_function(requests.post)
    

    This is the same as if you had written:

    class requests(object):
        @my_function
        def post(self, *args):
            pass
    

    Note that in your case requests is actually a module, not a class, but the same principles apply.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using twython to connect to Twitter API and retrieve a query. Here
I recently installed twython, a really sleek and awesome twitter API wrapper for Python.
Django advanced beginner here. I'm banging my head against the wall trying to figure
Hi i have just started to evaluate different options for python>Twitter api:s. I have
I'm trying to add 'Share on Twitter' functionality on one of the pages of
I'm writing a semi-generic form plugin using jQuery in order to speed up the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.