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

This is my first post here. I have a problem. I need to take
Now i have a problem need help from everyone: I want to take positions
Problem: I need to write/read objects from a file.This because I need to write/read
I have problem i need to convert from my Array structure to std::vector<int> ...
In my app I have programmable keyboard and I need to take input from
I have a problem i need to reference a single foreign key to a
An XSLT-newbie problem: I need to substitute a text value in XML-file. All other
To solve some problem I need to compute a variant of the pascal's triangle
I faced a problem - I need to use a macro value both as
i got some problem and need help .. my plan : 1. get ip

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.