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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:51:42+00:00 2026-06-10T21:51:42+00:00

I have a Python system consisting of around 9-10 classes all implementing most of

  • 0

I have a Python system consisting of around 9-10 classes all implementing most of a fat duck-typed interface and used interchangeably by a large collection of modules. I’m trying to refactor the classes into a core, explicit (i.e. ABC) interface and peripheral functionality, following separation of responsibility, but in order to do that I need to be able to tell when the consumer modules are calling methods outside the core interface.

Suppose I have an ABC with abstract methods:

from abc import ABCMeta, abstractmethod

class MyABC:
    __metaclass__ = ABCMeta
    @abstractmethod 
    def foo(self):
        pass

I also have a class implementing those abstract methods as well as other methods:

class MyClass(MyABC):
    def foo(self):
        pass
    def bar(self):
        pass

instance = MyClass()

>>> isinstance(instance, MyABC)
True

How can I ensure that when I pass instance to a method do_something it only uses the methods that are part of MyABC (in this case foo) and not any other methods (bar)? In a static-typed language (e.g. C++) I could pass do_something a pointer of the ABC type; is there some sort of wrapper available in Python that will restrict method access similarly?

  • 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-10T21:51:43+00:00Added an answer on June 10, 2026 at 9:51 pm

    This is what I came up with:

    class ABCGuard(object):
        def __init__(self, obj, *abcs):
            if any(not isinstance(obj, abc) for abc in abcs):
                raise ValueError("{0} must implement {1}"
                                 .format(obj.__class__.__name__,
                                         ', '.join(abc.__name__ for abc in abcs
                                                   if not isinstance(obj, abc))))
            self.__obj = obj
            self.__abcs = abcs
            classname = '{0}{{{1}}}'.format(obj.__class__.__name__,
                                            ', '.join(abc.__name__ for abc in abcs))
            self.__class__ = type(classname, (ABCGuard, ) + abcs, {})
    
        def __getattribute__(self, name):
            if name.startswith('_ABCGuard__') or (name.startswith('__') and
                                                  name.endswith('__')):
                return super(ABCGuard, self).__getattribute__(name)
            elif any(name in abc.__abstractmethods__ for abc in self.__abcs):
                return getattr(self.__obj, name)
            else:
                raise AttributeError("%r object has no attribute %r" %
                                     (self.__class__.__name__, name))
    
        def __dir__(self):
            return [x for abc in self.__abcs for x in abc.__abstractmethods__]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a Python system for which I develop, we usually have this module structure.
I have a machine control system in Python that currently looks roughly like this
With python inotifyx, do I have to remove watch and close opened system file
I have python list like below documents = [Human machine interface for lab abc
I have a problem with python-config --ldflags on OS X 10.6.2. Using my non-system
I am working on a RHEL system. I have two versions of Python installed
I have a python script to install/uninstall some regularly used programs for me and
I have a python program in an embedded system that needs to write to
I have this python code: import os try: os.system('wrongcommand') except: print(command does not work)
I have the following python code: os.system(C:/Python27/python.exe C:/GUI/TestGUI.py) sys.exit(0) It runs the command fine,

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.