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 6058373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:35:33+00:00 2026-05-23T08:35:33+00:00

Is it possible to implement generic method handlers in python which allow for calling

  • 0

Is it possible to implement generic method handlers in python which allow for calling of non-existent functions? Something like this:

class FooBar:
  def __generic__method__handler__(.., methodName, ..):
    print methodName

fb = FooBar()
fb.helloThere()

-- output --
helloThere
  • 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-23T08:35:33+00:00Added an answer on May 23, 2026 at 8:35 am

    The first thing to remember is that methods are attributes which happen to be callable.

    >>> s = " hello "
    >>> s.strip()
    'hello'
    >>> s.strip
    <built-in method strip of str object at 0x000000000223B9E0>
    

    So you can handle non-existent methods in the same way you would handle non-existent attributes.

    This is usally done by defining a __getattr__ method.

    Now you’re going hit the additional complexity which is the difference between functions and method. Methods need to be bound to an object. You can take a look at this question for a discussion of this.

    So I think you’ll want something like this:

    import types
    
    class SomeClass(object):
        def __init__(self,label):
            self.label = label
    
        def __str__(self):
            return self.label
    
        def __getattr__(self, name):
            # If name begins with f create a method
            if name.startswith('f'):
                def myfunc(self):
                    return "method " + name + " on SomeClass instance " + str(self)
                meth = types.MethodType(myfunc, self, SomeClass)
                return meth
            else:
                raise AttributeError()
    

    Which gives:

    >>> s = SomeClass("mytest")
    >>> s.f2()
    'method f2 on SomeClass instance mytest'
    >>> s.f2
    <bound method SomeClass.myfunc of <__main__.SomeClass object at 0x000000000233EC18>>
    

    However, I’d probably recommend against using this. If you tell us the problem you’re trying to solve I expect someone here can come up with a better solution.

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

Sidebar

Related Questions

Hopefully I've described it correctly. I have a 'generic method' which looks like the
Is it possible to implement McCarthy's amb -operator for non-deterministic choice in C#? Apparently
It's possible to implement INotifyCollectionChanged or other interface like IObservable to enable to bind
Using Java Generics, I tried to implement a generic console input method. public static
I don't think this is possible but here goes... I want to add method
I want to implement a generic method to retrieve header/detail data from a database:
Imagine C# code like this ISomeInterface someVar = creator.Create(typeof(SomeClass), typeof(ISomeInterface)); What should happen? Method
I'm trying to build an object that looks something like this: public class MyObject
Is there a way to implement this pattern in a generic way? A dispatcher
I'm looking for a generic method to implement a wait screen during long operations.

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.