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

  • Home
  • SEARCH
  • 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 8033139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:36:15+00:00 2026-06-05T01:36:15+00:00

A certain situation in Python recently alarmed me, and its reason is still not

  • 0

A certain situation in Python recently alarmed me, and its reason is still not completely clear after a little research. The following class definitions appear to work flawlessly and will produce what is intended:

class A: __slots__ = 'a', 'b'
class B(A): __slots__ = ()
class C(A): __slots__ = ()
class D(B, C): __slots__ = ()

These are four classes arranged in a diamond inheritance pattern. However, a somewhat similar pattern is not allowed. The following class definitions seem as though they should function the same as the first:

class B: __slots__ = 'a', 'b'
class C: __slots__ = 'a', 'b'
class D(B, C): __slots__ = ()

Traceback (most recent call last):
  File "<pyshell#74>", line 1, in <module>
    class D(B, C): __slots__ = ()
TypeError: multiple bases have instance lay-out conflict

However, a TypeError is raised in this example. So three questions arise: (1) Is this a bug in Python, considering the slot names? (2) What would justify such an answer? (3) What is the best workaround?


References:

  • __slots__ and multiple inheritance (with replies following)
  • Multiple inheritance and __slots__
  • Don’t use __slots__
  • Multiple Inheritance __slots__ problem
  • 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-06-05T01:36:17+00:00Added an answer on June 5, 2026 at 1:36 am

    By forcing a constraint that no class defines __slots__, a special object class could be constructed with the characteristics desired for all child classes. The class is registered as an alias for regular objects.

    class _object: __slots__ = '_MetaSafe__exec', '__dict__'
    
    class MetaSafe(type):
    
        __REGISTRY = {object: _object}
    
        @classmethod
        def clone(cls, old):
            return cls(old.__name__, old.__bases__, dict(old.__dict__), old)
    
        def __new__(cls, name, bases, classdict, old=None):
            # Check on a few classdict keys.
            assert '__new__' not in classdict, '__new__ must not be defined!'
            assert '__slots__' not in classdict, '__slots__ must not be defined!'
            assert '__module__' in classdict, '__module__ must be defined!'
            # Validate all the parent classes.
            valid = []
            for base in bases:
                if base in cls.__REGISTRY:
                    valid.append(cls.__REGISTRY[base])
                elif base in cls.__REGISTRY.values():
                    valid.append(base)
                else:
                    valid.append(cls.clone(base))
            # Wrap callables without thread mark.
            for key, value in classdict.items():
                if callable(value):
                    classdict[key] = cls.__wrap(value)
            # Fix classdict and create new class.
            classdict.update({'__new__': cls.__new, '__slots__': (), '__module__':
                              '{}.{}'.format(__name__, classdict['__module__'])})
            cls.__REGISTRY[old] = new = \
                super().__new__(cls, name, tuple(valid), classdict)
            return new
    
        def __init__(self, name, bases, classdict, old=None):
            return super().__init__(name, bases, classdict)
    
        @staticmethod
        def __wrap(func):
            @functools.wraps(func)
            def safe(self, *args, **kwargs):
                return self.__exec(func, self, *args, **kwargs)
            return safe
    
        @classmethod
        def __new(meta, cls, *args, **kwargs):
            self = object.__new__(cls, *args, **kwargs)
            if 'master' in kwargs:
                self.__exec = kwargs['master'].__exec
            else:
                array = tuple(meta.__REGISTRY.values())
                for value in args:
                    if isinstance(value, array):
                        self.__exec = value.__exec
                        break
                else:
                    self.__exec = Affinity()
            return self
    

    This code can be used as a building block to make tkinter thread-safe by cloning its classes. The Affinity class automatically ensures that code is executed on a single thread, preventing GUI errors.

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

Sidebar

Related Questions

I have the following situation: I have a certain function that runs a loop
I have the following situation: A user will define a certain filter on a
I have the following situation... In a certain View, the user must select the
I am trying to create some rules and facts on a certain situation. The
Situation: I have some persons with certain skills and they can/might belong to more
I have a situation where I want certain code to be executed no matter
I'm almost certain about the answer, but the situation is so critical that I
Here's the situation: I wrote a back end application, that runs on a certain
After certain R#-recommended edits R# colors the background of blocks of code in a
After competing in and following this year's Google Code Jam competition, I couldn't help

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.