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

The Archive Base Latest Questions

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

The code is as below, just the basic structure: class FooType(type): def __new__( cls,

  • 0

The code is as below, just the basic structure:

class FooType(type):
    def __new__( cls, name, bases, classdict ):
        instance = type.__new__( cls, name, bases, classdict )
        # What can I do here?
        return instance

class FooBase( object, metaclass=FooType ):
    def __init__( self ):
        pass

class Foo( FooBase ):
    def __init__( self, name ):
        self.name = name

    def method1( self ):
        pass

    def method2( self ):
        pass

    def specialmethod( self ):
        pass

class A( Foo ):
    pass

class B( Foo ):
    pass

class C( Foo ):
    _disallowed_methods = ['specialmethod']

What I want to do is that instances of class C should not have specialmethod, but that method should be available to instances A and B.

I can override this method in class C and raise an error, but I would prefer not to do this.

I realize I can add in code to check for _disallowed_methods in the FooType and on the basis of that check if the instance has any of them in the output of dir(instance). But I cannot seem to remove the method from __dict__ of C using any methods I have tried so far. The methods I tried are delattr(instance, 'specialmethod'), and del instance.__dict__['specialmethod'].

The delattr method results in “AttributeError: specialmethod”, and the del method results in “TypeError: ‘dict_proxy’ object does not support item deletion”

Basically many different classes will inherit from Foo, but some of them should not have specific methods available to them like C which should not have specialmethod available to it.

What am I doing wrong? Or how else can I accomplish this?

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

    Or how else can I accomplish this?

    You could achieve similar results by using multiple inheritance.

    Move the methods that you would like only some of the children to have from Foo to ExtraFoo. Then use class A(Foo, ExtraFoo) or class C(Foo). This way you could even “reattach” a given method further down the children hierarchy.

    If reattaching the method is not something you are interested to, then you could simply have ExtraFoo as a child of Foo (so: adding the methods, not detaching them) and have class A(ExtraFoo) and class C(Foo).

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

Sidebar

Related Questions

I am just learning Perl's comparison operators. I tried the below code :- $foo=291;
I grabbed this code form JCarousel and just trying to understand these lines below.
Code below is working well as long as I have class ClassSameAssembly in same
The code below gives an error: Property 'Int32 Key' is not defined for type
We have an issue whereby we are using this code below. Its just a
This is probably unbelievably basic. In the code below, I have annotated the part
See code just bellow Our generic interface public interface Repository<INSTANCE_CLASS, INSTANCE_ID_CLASS> { void add(INSTANCE_CLASS
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
The code below shows a sample that I've used recently to explain the different

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.