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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:38:10+00:00 2026-05-20T09:38:10+00:00

Using metaclasses, I am trying to create an instance method by simplifying an existing

  • 0

Using metaclasses, I am trying to create an instance method by simplifying an existing instance method. The problem is that partial does not work with instance method. This is a simple example of what I try to achieve:

from functools import partial

class Aclass(object):

    def __init__(self, value):
        self._value = value

    def complex(self, a, b):                                            
        return a + b + self._value

class Atype(type):

    def __new__(cls, name, bases, attrs):
        return super(Atype, cls).__new__(cls, name, (Aclass, ) + bases, attrs)

    def __init__(cls, name, bases, attrs):
        setattr(cls, 'simple', partial(cls.complex, b=1))

class B(metaclass=Atype):
    pass

b = B(10)

print(b.complex(1, 2))
print(b.simple(1))

and the output is:

13
Traceback (most recent call last):
  File "metatest.py", line 22, in <module>
    print(b.simple(1))
TypeError: complex() takes exactly 3 non-keyword positional arguments (1 given)

I have solved using lambda changing:

    setattr(cls, 'simple', partial(cls.complex, b=1))

to:

    setattr(cls, 'simple', lambda self, x: cls.complex(self, x, b=1))

but it is ugly and has problems with optional parameters.

I could create these method at the instance __init__ but I guess it makes more sense, and is more efficient to do it on class __init__using metaclasses.

Any ideas how to do it properly?

  • 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-20T09:38:10+00:00Added an answer on May 20, 2026 at 9:38 am

    Well, I am a bit unfamiliar with Python 3 method handling yet –
    the simplest thing I could think of is rewriting partial so that it preserves the first argument from the original call, then inserts the “partial” parameters.

    It worked with your example, but it needs testing with more complex patterns.

    from functools import wraps
    
    class Aclass(object):
        def __init__(self, value):
            self._value = value
    
        def complex(self, a, b):                                            
            return a + b + self._value
    
    def repartial(func, *parameters, **kparms):
        @wraps(func)
        def wrapped(self, *args, **kw):
            kw.update(kparms)
            return func(self, *(args + parameters), **kw)
        return wrapped
    
    class Atype(type):
        def __new__(cls, name, bases, attrs):
            return super(Atype, cls).__new__(cls, name, (Aclass, ) + bases, attrs)
    
        def __init__(cls, name, bases, attrs):
            setattr(cls, 'simple', repartial(cls.complex, b=1))
    
    class B(metaclass=Atype):
        pass
    
    b = B(10)
    
    print(b.complex(1, 2))
    print(b.simple(1))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to dynamically create domain objects in Grails and encountered the problem that
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using C#, I need a class called User that has a username, password, active
I am trying to decorate the magic method __getitem__ to be a classmethod on
I found a lot of links on metaclasses, and most of them mention that
I'm using base class constructor as factory and changing class in this constructor/factory to
I'm implementing enumeration using a base class that defines a variety of methods. The
Using PHP, I'm trying to give each specific text its own variable. I believe

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.