How to write a function to return the variable name?

后端 未结 6 1350
自闭症患者
自闭症患者 2020-12-04 03:59

I want a function that can return the variable/object name as str like this :

def get_variable_name (input_variable):
    ## some codes

>>get_variable         


        
相关标签:
6条回答
  • 2020-12-04 04:38

    If your statement to be used in exec() is something like this

    a = ["ffffd","dfd","444"]
    

    then do something like this

    exec('b = a = ["ffffd","dfd","444"]')
    

    now you can use 'b' in your code to get a handle on 'a'.

    0 讨论(0)
  • 2020-12-04 04:42

    Perhaps you can use traceback.extract_stack() to get the call stack, then extract the variable name(s) from the entry?

    def getVarName(a):
        stack = extract_stack()
        print(stack.pop(-2)[3])
    
    bob = 5
    getVarName(bob);
    

    Output:

    getVarName(bob)
    
    0 讨论(0)
  • 2020-12-04 04:48
    def getvariablename(vara):
        for k in globals():
            if globals()[k] == vara:
                      return k
         return str(vara)
    

    may work in some instance ...but very subject to breakage... and I would basically never use it in any kind of production code...

    basically I cant think of any good reason to do this ... and about a million not to

    0 讨论(0)
  • 2020-12-04 04:53

    I've seen a few variants on this kind of question several times on SO now. The answer is don't. Learn to use a dict anytime you need association between names and objects. You will thank yourself for this later.

    In answer to the question "How can my code discover the name of an object?", here's a quote from Fredrik Lundh (on comp.lang.python):

    The same way as you get the name of that cat you found on your porch: the cat (object) itself cannot tell you its name, and it doesn’t really care — so the only way to find out what it’s called is to ask all your neighbours (namespaces) if it’s their cat (object)…

    ….and don’t be surprised if you’ll find that it’s known by many names, or no name at all!


    Note: It is technically possible to get a list of the names which are bound to an object, at least in CPython implementation. If you're interested to see that demonstrated, see the usage of the inspect module shown in my answer here:

    Can an object inspect the name of the variable it's been assigned to?

    This technique should only be used in some crazy debugging session, don't use anything like this in your design.

    0 讨论(0)
  • 2020-12-04 04:59

    In general it is not possible. When you pass something to a function, you are passing the object, not the name. The same object can have many names or no names. What is the function supposed to do if you call get_variable_name(37)? You should think about why you want to do this, and try to find another way to accomplish your real task.

    Edit: If you want get_variable_name(37) to return 37, then if you do a=37 and then do get_variable_name(a), that will also return 37. Once inside the function, it has no way of knowing what the object's "name" was outside.

    0 讨论(0)
  • 2020-12-04 05:01

    if you just want to return the name of a variable selected based on user input... so they can keep track of their input, add a variable name in the code as they make selections in addition to the values generated from their selections. for example:

    temp = raw_input('Do you want a hot drink?  Type yes or no. ')
    size = raw_input('Do you want a large drink? Type yes or no. ')
    if temp and size == 'yes':
        drink = HL
        name = 'Large cafe au lait'
    if temp and size != 'yes':
        drink = CS
        name = 'Small ice coffee'
    print 'You ordered a ', name, '.'
    

    MJ

    0 讨论(0)
提交回复
热议问题