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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:43:49+00:00 2026-05-23T16:43:49+00:00

I’m simulating a distributed system in which all nodes follow some protocol. This includes

  • 0

I’m simulating a distributed system in which all nodes follow some protocol. This includes assessing some small variations in the protocol. A variation means alternative implementation of a single method. All nodes always follow the same variation, which is determined by experiment configuration (only one configuration is active at any given time). What is the clearest way to do it, without sacrificing performance?

As an experiment can be quite extensive, I clearly don’t want any conditionals. Before I’ve just used inheritance, like:

class Node(object):

    def dumb_method(self, argument):
        # ...    

    def slow_method(self, argument):
        # ...

    # A lot more methods

class SmarterNode(Node):

    def dumb_method(self, argument):
        # A somewhat smarter variant ...

class FasterNode(SmarterNode):

    def slow_method(self, argument):
        # A faster variant ...

But now I need to test all possible variants and don’t want an exponential number of classes cluttering the source. I also want other people peeping at the code to understand it with minimal effort. What are your suggestions?

Edit: One thing I failed to emphasize enough: for all envisioned use cases, it seems that patching the class upon configuration is good. I mean: it can be made to work by simple Node.dumb_method = smart_method. But somehow it didn’t feel right. Would this kind of solution cause major headache to a random smart reader?

  • 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-23T16:43:50+00:00Added an answer on May 23, 2026 at 4:43 pm

    You can create new subtypes with the type function. You just have to give it the subclasses namespace as a dict.

    # these are supposed to overwrite methods
    def foo(self):
        return "foo"
    
    def bar(self):
        return "bar"
    
    
    def variants(base, methods):
        """
            given a base class and list of dicts like [{ foo = <function foo> }] 
             returns types T(base) where foo was overwritten
        """
        for d in methods:
            yield type('NodeVariant', (base,), d)
    
    
    from itertools import combinations
    def subdicts(**fulldict):
        """ returns all dicts that are subsets of `fulldict` """
        items = fulldict.items()
        for i in range(len(items)+1):
            for subset in combinations(items, i):
                yield dict(subset)
    
    # a list of method variants
    combos = subdicts(slow_method=foo, dumb_method=bar)
    
    # base class
    class Node(object):
            def dumb_method(self):
                return "dumb"
            def slow_method(self):
                return "slow"
    
    # use the base and our variants to make a number of types
    types = variants(Node, combos)
    
    # instantiate each type and call boths methods on it for demonstration
    print [(var.dumb_method(), var.slow_method()) for var
              in (cls() for cls in types)]
    
    # [('dumb', 'slow'), ('dumb', 'foo'), ('bar', 'slow'), ('bar', 'foo')]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.