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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:28:30+00:00 2026-05-27T22:28:30+00:00

In Python, how can I get the containing context of an object (a function,

  • 0

In Python, how can I get the containing context of an object (a function, in my case), so I can modify it? For example, if it’s a method, its context is the class; if it’s a free function, its context is the module.

Related question:
Can a Python decorator of an instance method access the class?

  • 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-27T22:28:31+00:00Added an answer on May 27, 2026 at 10:28 pm

    I think you’re going to need to roll your own version, but the standard-library inspect module will get you most of what you need here.

    inspect.ismethod() and inspect.isfunction() will help determine if the function is bound (defined in a class) or not, and thus whether you’re looking for a class or a module (you hope… cause a function might be defined in another scope than a module… e.g. as a locally defined function within scope of another function or method, or as a lambda (same thing really))

    Note in particular that the im_class of a method won’t retrieve you the class the method was defined in. But you can (using inspect.getmro()) follow the class hierarchy and find the ‘most recent’ class that the method was defined in.

    BTW – I’m wondering whether modifying a module will be what you want… in many cases the names within that module may already be imported elsewhere…) In particular I’d be worried (for you and your future sanity) if you have ruby metaprogramming experience and are trying to replicate that in python – it’s very different and best not to try to do rubyish things in python.

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

Sidebar

Related Questions

I can't seem to get Python to import a module in a subfolder. I
I can't seem to get the wsgiref module to work at all under Python
Is there any way I can get my Python code syntax highlighted in Microsoft
is there a python library for supporting OpenType features? where can i get it?
In Python, how big can a list get? I need a list of about
How can I iterate over a string in Python (get each character from the
In Python, how can I simply do the equivalent of dictionary.get(key, default) for lists
I am developing in python a file class that can read and write a
I am trying to get completely to grips with class inheritence in Python. I
How can I make python's unittest module show output for every assertion, rather than

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.