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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:54:33+00:00 2026-05-11T03:54:33+00:00

The problem is a need to take the arguments into account before choosing the

  • 0

The problem is a need to take the arguments into account before choosing the responder. Here is my attempt so far.

from responders import A, B, C  class RandomResponder(object)     def init(self, *args, *kwargs):         self.args = args         self.kwargs = kwargs      def __getattr__(self, name):         # pick a responder based on the args the function was called with         # I don't know how to do this part         # for sake of argument lets the args a function was called with lead me to pick responder A         r = A         responder = r(*self.args, **self.kwargs)         return responder.__getattr__(name) 

The desired effect would be:

r = RandomResponder() r.doSomething(1) #returns A.doSomething() r.doSomething(2) #returns B.doSomething() r.doSomething(3) #return C.doSomething() r.doSomethingElse(1) #returns A.doSomethingElse() r.doSomethingElse(2) #returns B.doSomethingElse() r.doSomethingElse(3) #returns C.doSomethingElse() 

I will not know ahead of time all the functions contained with the responders A, B, and C.

  • 1 1 Answer
  • 6 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. 2026-05-11T03:54:34+00:00Added an answer on May 11, 2026 at 3:54 am

    Try this:

    class RandomResponder(object):     choices = [A, B, C]      @classmethod     def which(cls):         return random.choice(cls.choices)      def __getattr__(self, attr):         return getattr(self.which(), attr) 

    which() randomly selects an option from the choices, and which getattr uses to get the attribute.

    EDIT: it actually looks like you want something more like this.

    class RandomResponder(object):     choices = [A, B, C]      def __getattr__(self, attr):         # we define a function that actually gets called         # which takes up the first positional argument,         # the rest are left to args and kwargs         def doCall(which, *args, **kwargs):             # get the attribute of the appropriate one, call with passed args             return getattr(self.choices[which], attr)(*args, **kwargs)         return doCall 

    This could be written using lambda, but I’ll just leave it like this so it’s clearer.

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

Sidebar

Related Questions

Right, so I have an enumerable and wish to get distinct values from it.
I've got a timestamp column in SQL Server 2008. Now I need to query
I need to be able to have boost::program_options parse an array of doubles that
In my program there is a BackgroundWorker class that preloads the images to BitmapImage
Again, asking MVC noob questions. Forgive my lack of experience. I have a situation
I am trying to understand tail-recursion in Haskell. I think I understand what it
I'm having some issues with the self parameter, and some seemingly inconsistent behavior in
In Scala, the interaction of overloading and implicit argument resolution seem to make it
I currently have an immutable type called Gene , that only has 2 fields:
Recently, I've been using one of the lesser-used STL features- custom allocators, and I

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.