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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:51:41+00:00 2026-06-01T17:51:41+00:00

Consider the following python code: def function(): Docstring name = ??? doc = ???

  • 0

Consider the following python code:

def function():
    "Docstring"

    name = ???
    doc = ???

    return name, doc

>>> function()
"function", "Docstring"

What do I need to replace the question marks with so that I get the name and the docstring of the function from inside the same function?

EDIT:
Most of the answers so far explicitly hardcode the name of the function inside its definition. Is it possible do something like below where a new function get_name_doc would access the function from the outer frame from which it is called, and return its name and doc?

def get_name_doc():
    ???

def function():
    "Docstring"

    name, doc = get_name_doc()

    return name, doc

>>> function()
"function", "Docstring"
  • 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-01T17:51:42+00:00Added an answer on June 1, 2026 at 5:51 pm

    This is not possible to do cleanly in a consistent way because names can be changed and reassigned.

    However, you can use this so long as the function isn’t renamed or decorated.

    >>> def test():
    ...     """test"""
    ...     doc = test.__doc__
    ...     name = test.__name__
    ...     return doc, name
    ... 
    >>> test()
    ('test', 'test')
    >>> 
    

    It’s not at all reliable. Here’s an example of it going wrong.

    >>> def dec(f):
    ...     def wrap():
    ...         """wrap"""
    ...         return f()
    ...     return wrap
    ... 
    >>> @dec
    ... def test():
    ...     """test"""
    ...     return test.__name__, test.__doc__
    ... 
    >>> test()
    ('wrap', 'wrap')
    >>> 
    

    This is because the name test isn’t defined at the time that the function is actually created and is a global reference in the function. It hence gets looked up in the global scope on every execution. So changes to the name in the global scope (such as decorators) will break your code.

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

Sidebar

Related Questions

Consider the following code in Python: class A(object): CLASS_ATTRIBUTE = 42 def f(self): return
consider the following python code: import gtk class MainWindow(): def __init__(self): self.window = gtk.Window()
consider the following python code class Base(object): def __init__(self): self.foo = 5 class Derived(Base):
Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo()
Consider the following code: def CalcSomething(a): if CalcSomething._cache.has_key(a): return CalcSomething._cache[a] CalcSomething._cache[a] = ReallyCalc(a) return
Consider the following Python code: def f(*args): for a in args: pass foo =
consider I have the following code in python module a.py : def func(): obj
In Python, consider I have the following code: class SuperClass(object): def __init__(self, x): self.x
Consider the following Python code: import os print os.getcwd() I use os.getcwd() to get
Possible Duplicate: Python: Retrieve items from a set Consider the following code: >>> item1

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.