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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:08:40+00:00 2026-05-22T02:08:40+00:00

Inspired by Muhammad Alkarouri answer in What are good uses for Python3's "Function Annotations"

  • 0

Inspired by Muhammad Alkarouri answer in What are good uses for Python3's "Function Annotations" , I want to do this multimethod for methods, not regular functions. However, when I do this

registry = {}

class MultiMethod(object):
    def __init__(self, name):
        self.name = name
        self.typemap = {}
    def __call__(self, *args):
        types = tuple(arg.__class__ for arg in args) # a generator expression!
        function = self.typemap.get(types)
    if function is None:
        raise TypeError("no match")
    return function(*args)
def register(self, types, function):
    if types in self.typemap:
        raise TypeError("duplicate registration")
    self.typemap[types] = function

def multimethod(function):
    name = function.__name__
    mm = registry.get(name)
    if mm is None:
        mm = registry[name] = MultiMethod(name)
    types = tuple(function.__annotations__.values())
    mm.register(types, function)
    return mm

class A:
@multimethod
def foo(self, a: int):
    return "an int"

a = A() 
print( a.foo( 1 ) ) 

I got this:

Traceback (most recent call last):
  File "test.py", line 33, in <module>
    print( a.foo( 1 ) )
  File "test.py", line 12, in __call__
    return function(*args)
TypeError: foo() takes exactly 2 arguments (1 given)

Which seems to be expected, as explained in Decorating a method , because of the self argument.

But I have no idea how to make it work. Well, when I remove the “self”, it’s working (almost) fine, but I don’t want to remove it. Please note, that I’m doing this for practice, I know that there are some libs, providing method overloading.

What I tried:

  • very silly, but wanted to try – added parameter self in def multimethod( function ) – the same error

  • I thought about adding in the __init__ of class MultiMethod a third parameter – obj and stored self as member, but I can’t do this through multimethod as it is a function.

  • I don’t want to add parameters for the decorator, so this options (if possible at all) is ignored

I read several similar questions, but didn’t find what I was looking for. I’m pretty sure this is dummy question, but I ran out of ideas.

  • 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-22T02:08:41+00:00Added an answer on May 22, 2026 at 2:08 am

    The basic problem you have is that you use a class in place of a function. There is no mechanism to bind that class to the instance it’s called from, unlike a function where this happens automatically.

    In short, when you do a.foo( .. ) it returns a MultiMethod, but this object has no idea that it is supposed to be bound to a.

    You have to pass in the instance in some way or another. One easy way is to wrap it all in a function and let Python do it’s thing:

    registry = {}
    
    class MultiMethod(object):
        def __init__(self, name):
            self.name = name
            self.typemap = {}
    
        # self = a MultiMethod instance, instance = the object we want to bind to
        def __call__(self, instance, *args):
            types = tuple(arg.__class__ for arg in args) # a generator expression!
            function = self.typemap.get(types)
    
            if function is None:
                raise TypeError("no match")
            return function(instance, *args)
    
        def register(self, types, function):
            if types in self.typemap:
                raise TypeError("duplicate registration")
            self.typemap[types] = function
    
    def multimethod(function):
        name = function.__name__
        mm = registry.get(name)
        if mm is None:
            mm = registry[name] = MultiMethod(name)
    
        types = tuple(function.__annotations__.values())
        mm.register(types, function)
        # return a function instead of a object - Python binds this automatically
        def getter(instance, *args, **kwargs):
            return mm(instance, *args, **kwargs)
        return getter
    
    class A:
        @multimethod
        def foo(self, a: int):
            return "an int", a
    
    a = A() 
    print( a.foo( 1 ) )
    

    The more complex way would be to write your own descriptor on the A class that does this binding.

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

Sidebar

Related Questions

inspired by this question since i do not find any good sql casts out
Inspired by this question and answer , how do I create a generic permutations
inspired by this answer , I wanted to inquire what other useful movement/editing commands
Inspired by the top answer on this page I wrote a python program to
Inspired by this CodingHorror article, Protecting Your Cookies: HttpOnly How do you set this
Inspired by another question asking about the missing Zip function: Why is there no
Inspired by this question , I wanted to try my hand at the latest
Inspired by this question I began wondering why the following examples are all illegal
This question was inspired by a similar question: How does delete[] know the size
Inspired by this question why do we need user defined classloader in java Does

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.