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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:46:27+00:00 2026-06-03T11:46:27+00:00

I have some code which I would like to pass instances or classes interchangeably.

  • 0

I have some code which I would like to pass instances or classes interchangeably. All I will do in that code is to call a method that I expect both classes and instances to have (the method go() in the example below).

Unfortunately, I can’t create a classmethod with the same name of a regular method… See example below. I initially expected the second call to produce an a instead of a b.

Any advice on how to achieve this?

Type "help", "copyright", "credits" or "license" for more information.
>>> class A(object):
...     def go(self):
...             print "a"
...     @classmethod
...     def go(cls):
...             print "b"
... 
>>> a=A()
>>> a.go()
b
>>> A.go()
b
  • 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-03T11:46:31+00:00Added an answer on June 3, 2026 at 11:46 am

    Consider reusing the classinstancemethod decorator from formencode.

    https://bitbucket.org/formencode/official-formencode/src/06d52c5b33c9/formencode/declarative.py

    class classinstancemethod(object):
        """
        Acts like a class method when called from a class, like an
        instance method when called by an instance.  The method should
        take two arguments, 'self' and 'cls'; one of these will be None
        depending on how the method was called.
        """
    
        def __init__(self, func):
            self.func = func
    
        def __get__(self, obj, type=None):
            return _methodwrapper(self.func, obj=obj, type=type)
    
    
    class _methodwrapper(object):
    
        def __init__(self, func, obj, type):
            self.func = func
            self.obj = obj
            self.type = type
    
        def __call__(self, *args, **kw):
            assert 'self' not in kw and 'cls' not in kw, (
                "You cannot use 'self' or 'cls' arguments to a "
                "classinstancemethod")
            return self.func(*((self.obj, self.type) + args), **kw)
    
        def __repr__(self):
            if self.obj is None:
                return ('<bound class method %s.%s>'
                        % (self.type.__name__, self.func.func_name))
            else:
                return ('<bound method %s.%s of %r>'
                        % (self.type.__name__, self.func.func_name, self.obj))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some auto-instantiation code which I would like to apply to about 15
I have some code that uses multi-methods and would ideally like to overload the
I have some code which builds an array of date ranges. I then call
I have some code which is been running by a backgroundworker I'd like some
I have the following code which turns a string, that I pass into the
Presently I have a code that has a java main method and which takes
I have some code (which I cannot change) that I need to get working
I have some code which populates a hashtable with a question as the key
I have some code which handles data files and reports an error when it
We have some code which sorts a list of addresses based on the distance

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.