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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:19:48+00:00 2026-06-18T00:19:48+00:00

What I want to do is something like: class Foo(object): def __init__(self): pass def

  • 0

What I want to do is something like:

class Foo(object):
    def __init__(self):
        pass
    def f(self):
        print "f"
    def g(self):
        print "g"


# programatically set the "default" operation
fer=Foo()
fer.__call__=fer.f

# a different instance does something else as its
# default operation
ger=Foo()
ger.__call__=ger.g

fer()  # invoke different functions on different
ger()  # objects depending on how they were set up.

But as of 2.7 (which I’m currently using) I can’t do this, the attempts at fer()
raise an exception.

Is there a way to, in effect, set a per instance __call__ method?

  • 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-18T00:19:49+00:00Added an answer on June 18, 2026 at 12:19 am

    The normal stuff with types.MethodType unfortunately doesn’t work here since __call__ is a special method.

    From the data model:

    Class instances are callable only when the class has a __call__() method; x(arguments) is a shorthand for x.__call__(arguments).

    This is slightly ambiguous as to what is actually called, but it’s clear that your class needs to have a __call__ method.

    You’ll need to create some sort of hack:

    class Foo(object):
        def __init__(self):
            pass
        def f(self):
            print "f"
        def g(self):
            print "g"
    
        def __call__(self):
            return self.__call__()
    
    f = Foo()
    f.__call__ = f.f
    f()
    
    g = Foo()
    g.__call__ = g.g
    g()
    

    Careful with this though, it’ll result in an infinite recursion if you don’t set a __call__ on an instance before you try to call it.

    Note that I don’t actually recommend calling the magic attribute that you rebind __call__. The point here is to demonstrate that python translates: f() into f.__class__.__call__(f) and so there’s nothing you can do to change it on a per-instance basis. the class’s __call__ will be called no matter what you do — You just need to do something to change the behavior of the class’s __call__ per-instance which is easily achieved.


    You could use a setter type thing to actually create methods on your class (rather than simple functions) — and of course that could be turned into a property:

    import types
    class Foo(object):
        def __init__(self):
            pass
        def f(self):
            print "f"
        def g(self):
            print "g"
    
        def set_func(self,f):
            self.func = types.MethodType(f,self)
    
        def __call__(self,*args,**kwargs):
            self.func(*args,**kwargs)
    
    f = Foo()
    f.set_func(Foo.f)
    f()
    
    def another_func(self,*args):
        print args
    
    f.set_func(another_func)
    f(1,2,3,"bar")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like this: class Foo(object): def __init__(self, name): self._name =
When inheriting from two objects like these class Foo(object): def __init__(self,a): self.a=a class Bar(object):
I want to do something like this: class Dictable: def dict(self): raise NotImplementedError class
If you have the following class: class Foo(object): def __init__(name): self.name = name And
Like I would want to do something like this, class Object { public: World
Essentially I want to do something like this: class foo: x = 4 @property
I want to do something like this: class Cls { function fun($php) { return
I'm having a class with a list of constants. I want something like get_object_vars
I want to do something like this: public class ScadenzaService { ... public List<Scadenza>
I want to do something like this: template<template<int d, class> class container, int dim

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.