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

  • Home
  • SEARCH
  • 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 6895887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:57:57+00:00 2026-05-27T06:57:57+00:00

I have about 20 methods to redirect to a wrapper method that takes the

  • 0

I have about 20 methods to redirect to a wrapper method that takes the original method, and the rest of the arguments:

class my_socket(parent):

    def _in(self, method, *args, **kwargs):
        # do funky stuff

    def recv(self, *args, **kwargs):
        return self._in(super().recv, *args, **kwargs)

    def recv_into(self, *args, **kwargs):
        return self._in(super().recv_into, *args, **kwargs)

    # and so on...

How can I add more of these methods programmatically? This is about as far as I get before everything starts to look wrong:

for method in 'recv', 'recvfrom', 'recvfrom_into', 'recv_into', ...:
    setattr(my_socket, method, ???)

Can I do this by assigning within the class definition, or something else that feels more natural?

class my_socket(parent):

    def makes_recv_methods(name):
        # wraps call to name

    def recv_meh = makes_recv_methods('recv_meh')

I’d prefer to use __get__ and friends when possible over magic functions from types.

  • 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-05-27T06:57:58+00:00Added an answer on May 27, 2026 at 6:57 am

    I’d do it by running some code to generate the methods from a list after the class is defined – you could put this into a decorator.

    import functools
    
    def wrap_method(cls, name):
        # This unbound method will be pulled from the superclass.
        wrapped = getattr(cls, name)
        @functools.wraps(wrapped)
        def wrapper(self, *args, **kwargs):
            return self._in(wrapped.__get__(self, cls), *args, **kwargs)
        return wrapper
    
    def wrap_methods(cls):
        for name in cls.WRAP_ATTRS:
            setattr(cls, name, wrap_method(cls, name))
        return cls
    
    @wrap_methods
    class my_socket(parent_class):
        WRAP_ATTRS = ['recv', 'recvfrom'] # ... + more method names
    
        def _in(self, method, *args, **kwargs):
            # do funky stuff
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called Path for which there are defined about 10 methods,
Where can I get info about implementing my own methods that have the ellipsis
we had a heated discussion about a method name. We have a class User
i have a quetion about MVC routing. I have a controller method that is
I have a method that's about ten lines of code. I want to create
I have read about partial methods in the latest C# language specification , so
I have read this post about how to test private methods. I usually do
Referring to the first answer about python's bound and unbound methods here, I have
I have a question about Criteria method, one-to-many relation to the database, 'one' is
I have a doubt about orkut's new URL Writing method. Earlier the home page

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.