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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:59:26+00:00 2026-05-29T18:59:26+00:00

In our codebase there is Python and C++ married by SWIG. The C++ classes

  • 0

In our codebase there is Python and C++ married by SWIG. The C++ classes are sometimes given Python extensions like so:

%pythoncode %{

def DiscreteKey_baseData(self, baseData):
    pass

def DiscreteKey_asSet(self):
    pass

DiscreteKey.baseData = new_instancemethod(DiscreteKey_baseData, None, DiscreteKey)
DiscreteKey.asSet = new_instancemethod(DiscreteKey_asSet, None, DiscreteKey)

%}

or

%pythoncode %{

def ParmID_hash(parmID):
    return hash(str(parmID))

ParmID.__hash__ = ParmID_hash

%}

What is the difference between using new.instancemethod to attach a method to a class, vs. simply assigning it, as in the second example? Could the first example be changed to simply

DiscreteKey.baseData = DiscreteKey_baseData
DiscreteKey.asSet = DiscreteKey_asSet

? (note that baseData takes another argument)

Or is the second example actually deficient in some way, and should be using new_instancemethod too?

  • 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-29T18:59:29+00:00Added an answer on May 29, 2026 at 6:59 pm

    Assigning a function to a class attribute, and doing the same via new_instancemethod() (which I assume is a Swig alias for the method constructor, e.g. types.MethodType) are exactly equivalent. You can tell by checking type(DiscreteKey.baseData). Regardless of how the method was assigned, it will be <type 'instancemethod'>.

    The new_instancemethod() constructor is useful when assigning functions onto instances, however. This won’t work without the method “wrapper.”

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

Sidebar

Related Questions

There are a couple of files in our git-controlled codebase that I'd like to
I'm doing a review for our codebase, and there are many statements like this:
There are a few windows service projects in our codebase that follow this model,
In our codebase any classes that need to be saved are IXmlSerializable, this means
In our current codebase we are using MFC db classes to connect to DB2.
Since our entire build system is written in Python, I'm wondering if there is
Our codebase where I work is .NET 2.0. For our new assembly/DLLs/web applications I
When reviewing our codebase, I found an inheritance structure that resembles the following pattern:
Looking at our codebase some code is included in a project explicitly and is
A very small portion of our codebase is some legacy Java code. I'm trying

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.