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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:19:09+00:00 2026-05-13T08:19:09+00:00

class Class1(object): … class Class2(object): … class Class3(object): … class A(object): def _methA(parm1, parm2)

  • 0
class Class1(object):
    ...

class Class2(object):
    ...

class Class3(object):
    ...

class A(object):
    def _methA(parm1, parm2)
        ...

    def _methB(parm1, parm2)
        ...

    def _methC(parm1, parm2)
        ...

    def manager(parm1, method, params)
        ...
        if parm1.__class__.__name__==Class1.__name__:
            response = _methA(parm1, params)
        elif parm1.__class__.__name__==Class2.__name__:
            response = _methB(parm1, params)
        elif io_source.__class__.__name__==Class3.__name__:
            response = _methC(parm1, params)
        else:
            raise Exception, "Unsupported parm1"
        ...

I didn’t like the way that if/elif block in manager() looked and refactored it to this:

def manager(parm1, method, params)
    ...
    try:
        response = {
                Class1.__name__: lambda parm1, parms: _methA(parm1, parms),
                Class2.__name__: lambda parm1, parms: _methB(parm1, parms),
                Class3.__name__: lambda parm1, parms: _methC(parm1, parms)
                }[parm1.__class__.__name__](parm1, parms)
    except KeyError:
        raise Exception, "Unsupported parm1" 

But the fact that the code is still looking at class names bothers me – I really don’t know how to explain why… Should it bother me?

Is there a better way to write code to call a method in class A that, depending on the class of one of its parameters, triggers the calling of different methods in A?

PS. Sorry for the contrived example, but posting the actual code would make the question even more convoluted. I tried to distill the issue to its essence…

  • 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-13T08:19:09+00:00Added an answer on May 13, 2026 at 8:19 am

    That’s one of many wrong ways to implement polymorphism. You should never look at class names. Looking at class names should bother you because it means that you haven’t delegated the responsibility correctly.

    Move each method into the appropriate class.

    class Class1(object):
        def method( self, theA, params ):
            theA.methA( params )
    
    class Class2(object):
        def method( self, theA, params ):
            theA.methB( params )
    
    class Class3(object):
        def method( self, theA, params ):
            theA.methC( params )
    
    class A(object):
        def methA(parm1, parm2)
            ...
    
        def methB(parm1, parm2)
            ...
    
        def methC(parm1, parm2)
            ...
    
        def manager(parm1, method, params)
            ...
            param1.method( self, params )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code: class Class1 { private Class2 object; public Class1(Class2 obj) :
Say I have the folowing code: class Class1(object): def __init__(self): self.my_attr = 1 self.my_other_attr
Let's say I have this code: class class1(object): def __init__(self): #don't worry about this
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
I have a class: class MyClass(object): @property def myproperty(self): return 'hello' Using mox and
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
I create the following class: class Image(object): def __init__(self, extension, data, urls=None, user_data=None): self._extension
I have the following problem: I have the class: class Word(object): def __init__(self): self.id
I have a nested class, let's call it class1 and it has class2 inside
class Class1 { private static object consoleGate = new Object(); private static void Trace(string

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.