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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:33:13+00:00 2026-05-17T15:33:13+00:00

I want to have immutable types that can, ideally, sort out their own hashing

  • 0

I want to have immutable types that can, ideally, sort out their own hashing and equality, but can be easily subclassed. I started off using namedtuple:

class Command(namedtuple('Command', 'cmd_string')):

  def valid_msg(msg):
    return True

  def make_command(msg):
    if self.valid_msg(msg):
      return '%s:%s' % (self.cmd_string, msg)
    else:
      raise ValueError(INVALID_MSG)

…but this does not lend itself to subclassing. Directly subclassing this means that the name of the tuple remains the same (for printing… not such a big deal), but more importantly you can’t add fields:

class LimitedLengthCommand(Command):

  # I want to have self.length! Where does it go?

  def valid_msg(msg):
    return len(msg) <= self.length

Simply creating another named tuple (as per the docs) means I don’t inherit any methods!

What is the simplest and easiest way to do something like this? I intend to have multiple subclasses of Command (eg. hex literals, 1-or-0, etc), but nothing complicated. Playing nice with multiple inheritance is not essential.

  • 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-17T15:33:13+00:00Added an answer on May 17, 2026 at 3:33 pm

    Here’s a metaclass to do what you want (I think). It works by storing the methods to be inherited in a dictionary and manually inserting them into the new classes dictionary. It also stores the attribute string that gets passed to the namedtuple constructor and merges that with the attribute string from the subclass. It then passes that to namedtuple and returns a class that’s inherited from the resulting namedtuple with all appropriate methods in its dictionary. Because the metaclass is derived from abc.ABCMeta, you get working type checking for free. Here’s how constructing a couple of classes looks:

    class Foo(object):
        __metaclass__ = ImmutableMeta
        _attributes_ = 'a b'
    
        def sayhi(self):
            print "Hello from {0}".format(type(self).__name__)
    
    class Bar(Foo):
        _attributes_ = 'c'
    
        def saybye(self):
            print "Goodbye from {0}".format(type(self).__name__)
    

    Here’s the metaclass:

    import collections as co
    import abc
    
    class ImmutableMeta(abc.ABCMeta):
    
        _classes = {}
    
        def __new__(meta, clsname, bases, clsdict):
            attributes = clsdict.pop('_attributes_')
    
            if bases[0] is object:
                # 'new' class
                methods = clsdict
            else:
                # we're 'inheriting' from an existing class
                base = bases[0]
                attributes = meta._classes[base]['attributes'] + ' ' + attributes
                base_methods = meta._classes[base]['methods'].copy()
                base_methods.update(clsdict)
                methods = base_methods
    
            # construct the actual base class and create the return class
            new_base = co.namedtuple(clsname + 'Base', attributes)
            cls = super(ImmutableMeta, meta).__new__(meta, clsname, (new_base,),
                                                     methods)
    
            # register the data necessary to 'inherit' from the class
            # and make sure that it passes typechecking
            meta._classes[cls] = {'attributes': attributes,
                                  'methods': methods}
            if bases[0] is not object:
                base.register(cls)
            return cls
    

    And here’s some paltry test code.

    a = Foo(1, 2)
    a.sayhi()
    
    b = Bar(1, 2, 3)
    b.sayhi()  # 'inherited' from class Foo
    b.saybye()
    
    try:
        b.c = 1         # will raise an AttributeError
    except AttributeError:
        print "Immutable"
    
    print "issubclass(Bar, Foo): {0}".format(issubclass(Bar, Foo))
    
    try:
       d =  {b: 1}        # No problems
    except TypeError:
        print "Cant put it in a dict"
    else:
        print "Can put it in a dict"
    

    Hope that helps. If you would prefer not to attach every method to every class that is supposed to inherit it, you could also provide a default __getattr__ that looks through the metaclasses dictionary and finds the appropriate method out of that. That would require somehow hardcoding the baseclass into method, probably using a closure.

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

Sidebar

Related Questions

No related questions found

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.