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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:10:04+00:00 2026-06-14T13:10:04+00:00

I have some classes whose methods which I want to be able to add

  • 0

I have some classes whose methods which I want to be able to add to other classes on optionally. My initial solution was to use mixins, but this can get a bit ugly:

class Schedule(Enumerator, Humanizer, Inferer, ...):
    ...

So I thought hey, perhaps I could use class decorators to achieve the same effect.

@enumerator
@humanizer
@inferer
class Schedule(object):
    ...

And here is a sample of the decorator functions:

import inspect

def inferer(original_class):
    from myproj.lib.inferer import Inferer
    methods = inspect.getmembers(Inferer, predicate=inspect.ismethod)
    for method in methods:
        setattr(original_class, method[0], types.MethodTypes(method[1], original_class))
    return original_class

…which seems to add the methods and classmethods appropriately to decorated classes. However, when I call one of these added methods (or classmethods) on the decorated class, I get some errors.

For methods:

>>> Schedule().humanize()
TypeError: unbound method humanize() must be called with Humanizer instance as first argument (got type instance instead)

…which seems to indicate these are added as classmethods?

For classmethods:

>>> schedule = Schedule.infer(foo)
TypeError: infer() takes exactly 2 arguments (3 given)

Note the definition of infer:

class Inferer(object):
    @classmethod
    def infer(cls, dates):
        ...

I added some lines to infer to show what arguments it’s getting when called as Schedule.infer():

cls: <class 'myproj.lib.inferer.Inferer'>
dates: <class 'myproj.Schedule'>

So, my question:

What is going wrong in the decorator function to cause these added methods and classmethods to behave strangely? Or, better put, how to I modify the decorator function to handle these additions properly?

Please let me know if I can provide any clarification on any point.

  • 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-14T13:10:05+00:00Added an answer on June 14, 2026 at 1:10 pm

    Let’s say this was a good idea. This is one way you might achieve it. I can’t say that I would advise it, though.

    def horrible_class_decorator_factory(mixin):
        def decorator(cls):
            d = cls.__dict__.copy()
            bases = tuple([b for b in cls.__bases__ if b != object] + [mixin])
            return type(cls.__name__, bases, d)
        return decorator
    

    Now you can do something like this:

    class Inferer(object):
        @classmethod
        def foo(cls):
            return "bar" + cls.__name__
    
    inferer = horrible_class_decorator_factory(Inferer)
    
    @inferer
    class X(object):
        pass
    
    X.foo()
    "barX"
    

    I’m with the commenters on this one. Just because you can do something doesn’t mean you should.

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

Sidebar

Related Questions

I have some classes which extends a superclass, and in the JSP I want
I have some classes(call it Class A) which I would like to unit test
I have some classes which are parameterized via their constructor. I need to provide
I have a base class IStructure which is derived by many classes. Some of
I want to share some information between two classes (A and B), which are
I have some plugin type classes whose thread safety and exception handling I cannot
I have some classes that are used for Styling and all of them display
I have some classes that represent immutable objects (Quantity, Price, Probability). Is there some
I have some classes that, for one reason or another, cannot be or need
I have some classes in my app that don't require an ID to be

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.