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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:09:06+00:00 2026-05-28T02:09:06+00:00

I have a class in my Python module that frobs widgets. It has several

  • 0

I have a class in my Python module that frobs widgets. It has several ways to do this, but I don’t know ahead of time which ones, if any, will actually work, and I might figure out new ways to frob the widgets later. Also, frobbing widgets can be expensive. So I’d like to have an instance of my class be able to look at itself, find all of the methods that it has for frobbing widgets, and to start trying to frob widgets until it succeeds, at which point it should stop caring about the methods it hasn’t yet tried.

class WidgetFrobber:
    def simpleFrobAttempt(widget, data):
        # fastest way, might not work
    def cleverFrobAttempt(widget, data):
        # fiddly, fast, and doesn't always work
    def boringFrobAttempt(widget, data):
        # works most of the time, often slow
    def desperateFrobAttempt(widget, data):
        # doesn't always work, pathetically slow

With that in place, I’d like to define a method of the class that looks for methods named ^[a-z]+FrobAttempt$, makes a list of them, and tries to frob widgets until the widget is successfully frobbed (at which point it should stop caring about other methods) or it runs out of possible methods. Since it’s my code, I can make sure that the whateverFrobAttempt methods all have the same naming conventions and required arguments. It’s best if there’s some ordering to the list of methods, so that the ones with a better average speed get tried first, but it’s acceptable if they’re attempted in random-ish order.

Is there a sane way to do this such that when new weirdFrobAttempt methods are added, they’ll be tried automatically, or am I better off just maintaining a hardcoded list of such methods and iterating over that ?

  • 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-28T02:09:06+00:00Added an answer on May 28, 2026 at 2:09 am

    I like sven’s idea of having a register. This is hacky, but if storing a global list of method names is ok then we could do something like this:

    FROB_METHOD_REGISTER = []
    def frob_method(fn):
        def decorate_frob(fn):
            FROB_METHOD_REGISTER.add(fn.__name__)
            return fn
        return decorate_frob
    
    class WidgetFrobber:
        def get_frob_methods(self):
            return [getattr(self, method) for method in FROB_METHOD_REGISTER]
    
        @frob_method
        def simpleFrobAttempt(widget, data):
            # fastest way, might not work
        @frob_method
        def cleverFrobAttempt(widget, data):
            # fiddly, fast, and doesn't always work
        @frob_method
        def boringFrobAttempt(widget, data):
            # works most of the time, often slow
        @frob_method
        def desperateFrobAttempt(widget, data):
            # doesn't always work, pathetically slow
    

    so then you can do

    for method in my_widget_frobber.get_frob_methods():
        #do your thing
    

    Not sure if this is the best or most pythonic way, and I’d be tempted to make the decorator use some sort of priority queue/ranking schema if there are some methods that are strictly better than others.

    You could use __dict__. for example

    [fn for name, fn in WidgetFrobber.__dict__.iteritems() if "FrobAttempt" in name]
    

    would give you the set of methods you are looking for, but I would not recommend using things like __dict__ if there is a way to build code that is more clear and doesn’t access python internals. And there almost always should be.

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

Sidebar

Related Questions

I have a Python 2.x module.py file that looks like this: class A(object): KEYWORD
I have a python module that defines a number of classes: class A(object): def
I have written a class in python that implements __str__(self) but when I use
I have some python module, which has a class ModuleClass and I can't modify
I have a module that looks something like this: def __myFunc(): ... class MyClass(object):
If I have a python class, how can I alias that class-name into another
Suppose I have a Python class that I want to add an extra property
so i've begun google's python class, and have not had that much difficulty with
When I import a module that has a class, what code is executed when
I have a module that imports a module, but in some cases the module

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.