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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:26:37+00:00 2026-06-18T03:26:37+00:00

Is it possible to get the name of the passed parameter in Python? Specifically,

  • 0

Is it possible to get the name of the passed parameter in Python?

Specifically, I want to be able to do this:

def a(name):
    print "the actual passed parameter is %s" %(???)

What it solves?

So if I need to distribute coins in bags and baskets, say bag_list and basket_list, I need not send an explicit identifier saying ‘bag’ or ‘basket’

Which is how I’ve currently worked around it. I’m currently using a (global) dictionary for this purpose-

ID = {'bag': bag_list, 'basket': basket_list}

def a(name, id):
   dist_list = ID[id]

It also boils down to converting a variable name to string because the desired behavior can also be modeled as:

def a(name):
    name = ???get variable name as string magically???
    if name.startswith('bag'): 
         dist_list = ID['bag']
    else:
         dist_list = ID['basket']

The re-modeled question has been answered comprehensively in https://stackoverflow.com/a/9121991/1397945 but I’m re-posting this

  • just in case there have been some modifications &
  • whether it is possible to do this in other scripting languages like Perl or Tcl.
  • More so, in case the two ways of modeling are different or can be approached any differently.

Thanks.

  • 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-18T03:26:38+00:00Added an answer on June 18, 2026 at 3:26 am

    It is possible – but I’d regard this rather as a dirty hack. After reading my solution, you should step back and think twice if you really want to go this way.

    def a(name):
        global_variables = globals()
        try:
            name_of_passed_in_variable = [x for x in global_variables if id(global_variables[x]) == id(name)][0]
        except Exception:
            name_of_passed_in_variable = "unknown"
        print name_of_passed_in_variable, name
    
    my_name = "Tom"
    a(my_name)
    
    a("Mike")
    
    second_name = my_name
    a(second_name)
    

    Will give the output:

    my_name Tom
    unknown Mike
    my_name Tom
    

    With my examples, you can also see the drawbacks of this approach.

    • If you pass in a string directly without assigning a variable, you cannot determine the name
    • If you have multiple references to the same variable, you’ll never know which name will be picked up

    For the latter problem you could of course implement some logic to cope with it – or maybe this is no problem for you at all.

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

Sidebar

Related Questions

Possible Duplicate: get name of a variable or parameter I have this function. public
Is it possible to get the original variable name of a variable passed to
Possible Duplicate: using substitute to get argument name with Note that this is -different-
Possible Duplicate: how to get value when a variable name is passed as a
I want to use passed template parameter as string. Is it possible? T is
Possible Duplicate: How to get the original variable name of variable passed to a
Possible Duplicate: get name of current PHP script in include file Can an included
Possible Duplicate: Get variable name. javascript “reflection” Is there a way to know the
Possible Duplicate: Get file name from URI string in C# How to extract file
Is it possible to get the name of a file downloaded with HttpURLConnection? URL

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.