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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:11:48+00:00 2026-06-06T20:11:48+00:00

Say there is: class A(B): … where B could be object and … is

  • 0

Say there is:

class A(B):
    ...

where B could be object and ... is not:

@classmethod # or @staticmethod
def c(cls): print 'Hello from c!'

What do I have to do that calling A.c() wont trigger AttributeError?

In other words, I know it is possible to manually add class methods to a class at runtime. But is it possible to do so automatically, say every time a class method is missing it creates some dummy method?

In yet another words, only if I could replace A.__dict__ with my dict which handles __getitem__ – but A.__dict__ seems to be not writable…

  • 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-06T20:11:50+00:00Added an answer on June 6, 2026 at 8:11 pm

    You can achieve this by using a __getattr__ hook on a metaclass.

    class DefaultClassMethods(type):
        def __getattr__(cls, attr):
            def _defaultClassMethod(cls):
                print 'Hi, I am the default class method!'
            setattr(cls, attr, classmethod(_defaultClassMethod))
            return getattr(cls, attr)
    

    Demo:

    >>> class DefaultClassMethods(type):
    ...     def __getattr__(cls, attr):
    ...         def _defaultClassMethod(cls):
    ...             print 'Hi, I am the default class method!'
    ...         setattr(cls, attr, classmethod(_defaultClassMethod))
    ...         return getattr(cls, attr)
    ... 
    >>> class A(object):
    ...     __metaclass__ = DefaultClassMethods
    ... 
    >>> A.spam
    <bound method DefaultClassMethods._defaultClassMethod of <class '__main__.A'>>
    >>> A.spam()
    Hi, I am the default class method!
    

    Note that we set the result of the classmethod call straight onto the class, effectively caching it for future lookups.

    If you need to regenerate the class method on every call instead, use the same method to bind a function to an instance but with the class and metaclass instead (using cls.__metaclass__ to be consistent with metaclass subclassing):

    from types import MethodType
    
    class DefaultClassMethods(type):
        def __getattr__(cls, attr):
            def _defaultClassMethod(cls):
                print 'Hi, I am the default class method!'
            return _defaultClassMethod.__get__(cls, cls.__metaclass__)
    

    For static methods just return the function directly in all cases, no need to muck with the staticmethod decorator or the descriptor protocol.

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

Sidebar

Related Questions

In my program, there are two class, say, category and product, with each object
Say you have a std::list of some class. There are two ways you can
In VB.NET there is a keyword 'shadows'. Let's say I have a base class
I have created a class to download images from web. Issue is there is
I have a question about how the System class works. Lets say there is
Lets say there is setup like this: public class MyClass { public void DoSomething(string
Say, there is a following example: class Super { public int i = 3;
Is there a clean method of mocking a class with generic parameters? Say I
Let's say I have these class hierarchy : public abstract class Parent { }
Let's say I have these models: class A(models.Model): name = models.CharField(max_length=32) b = models.ForeignKey(B,

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.