Simpler way to create dictionary of separate variables?

前端 未结 27 1922
名媛妹妹
名媛妹妹 2020-11-22 02:42

I would like to be able to get the name of a variable as a string but I don\'t know if Python has that much introspection capabilities. Something like:

>&         


        
相关标签:
27条回答
  • 2020-11-22 03:28

    I think my problem will help illustrate why this question is useful, and it may give a bit more insight into how to answer it. I wrote a small function to do a quick inline head check on various variables in my code. Basically, it lists the variable name, data type, size, and other attributes, so I can quickly catch any mistakes I've made. The code is simple:

    def details(val):
      vn = val.__name__                 #  If such a thing existed
      vs = str(val)
      print("The Value of "+ str(vn) + " is " + vs)
      print("The data type of " + vn + " is " + str(type(val)))
    

    So if you have some complicated dictionary / list / tuple situation, it would be quite helpful to have the interpreter return the variable name you assigned. For instance, here is a weird dictionary:

    m = 'abracadabra'
    mm=[]    
    for n in m:
      mm.append(n)
    mydic = {'first':(0,1,2,3,4,5,6),'second':mm,'third':np.arange(0.,10)}
    
    
    
    details(mydic)
    
    The Value of mydic is {'second': ['a', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a'], 'third': array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.]), 'first': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}
    The data type of mydic is <type 'dict'>
    
    details(mydic['first'])
    The Value of mydic['first'] is (0, 1, 2, 3, 4, 5, 6)]
    The data type of mydic['first'] is <type 'list'>
    
    details(mydic.keys())
    The Value of mydic.keys() is ['second', 'third', 'first']
    The data type of mydic.keys() is <type 'tuple'>
    
    details(mydic['second'][0])
    The Value of mydic['second'][0] is a
    The data type of mydic['second'][0] is <type 'str'>
    

    I'm not sure if I put this in the right place, but I thought it might help. I hope it does.

    0 讨论(0)
  • 2020-11-22 03:28

    I wrote a neat little useful function based on the answer to this question. I'm putting it here in case it's useful.

    def what(obj, callingLocals=locals()):
        """
        quick function to print name of input and value. 
        If not for the default-Valued callingLocals, the function would always
        get the name as "obj", which is not what I want.    
        """
        for k, v in list(callingLocals.items()):
             if v is obj:
                name = k
        print(name, "=", obj)
    

    usage:

    >> a = 4
    >> what(a)
    a = 4
    >>|
    
    0 讨论(0)
  • 2020-11-22 03:28

    I uploaded a solution to pypi. It's a module defining an equivalent of C#'s nameof function.

    It iterates through bytecode instructions for the frame its called in, getting the names of variables/attributes passed to it. The names are found in the .argrepr of LOAD instructions following the function's name.

    0 讨论(0)
  • 2020-11-22 03:30

    In reading the thread, I saw an awful lot of friction. It's easy enough to give a bad answer, then let someone give the correct answer. Anyway, here is what I found.

    From: [effbot.org] (http://effbot.org/zone/python-objects.htm#names)

    The names are a bit different — they’re not really properties of the object, and the object itself doesn't know what it’s called.

    An object can have any number of names, or no name at all.

    Names live in namespaces (such as a module namespace, an instance namespace, a function’s local namespace).

    Note: that it says the object itself doesn’t know what it’s called, so that was the clue. Python objects are not self-referential. Then it says, Names live in namespaces. We have this in TCL/TK. So maybe my answer will help (but it did help me)

    
        jj = 123
        print eval("'" + str(id(jj)) + "'")
        print dir()
    
    

    166707048
    ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'jj']
    

    So there is 'jj' at the end of the list.

    Rewrite the code as:

    
        jj = 123
        print eval("'" + str(id(jj)) + "'")
        for x in dir():
            print id(eval(x))
    
    
    161922920
    ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'jj']
    3077447796
    136515736
    3077408320
    3077656800
    136515736
    161922920
    

    This nasty bit of code id's the name of variable/object/whatever-you-pedantics-call-it.

    So, there it is. The memory address of 'jj' is the same when we look for it directly, as when we do the dictionary look up in global name space. I'm sure you can make a function to do this. Just remember which namespace your variable/object/wypci is in.

    QED.

    0 讨论(0)
  • 2020-11-22 03:31

    Maybe I'm overthinking this but..

    str_l = next((k for k,v in locals().items() if id(l) == id(v)))
    
    
    >>> bar = True
    >>> foo = False
    >>> my_dict=dict(bar=bar, foo=foo)
    >>> next((k for k,v in locals().items() if id(bar) == id(v)))
    'bar'
    >>> next((k for k,v in locals().items() if id(foo) == id(v)))
    'foo'
    >>> next((k for k,v in locals().items() if id(my_dict) == id(v)))
    'my_dict'
    
    0 讨论(0)
  • 2020-11-22 03:34

    I find that if you already have a specific list of values, that the way described by @S. Lotts is the best; however, the way described below works well to get all variables and Classes added throughout the code WITHOUT the need to provide variable name though you can specify them if you want. Code can be extend to exclude Classes.

    import types
    import math  # mainly showing that you could import what you will before d
    
    # Everything after this counts
    d = dict(globals())
    
    def kv_test(k,v):
        return (k not in d and 
                k not in ['d','args'] and
                type(v) is not types.FunctionType)
    
    def magic_print(*args):
        if len(args) == 0: 
            return {k:v for k,v in globals().iteritems() if kv_test(k,v)}
        else:
            return {k:v for k,v in magic_print().iteritems() if k in args}
    
    if __name__ == '__main__':
        foo = 1
        bar = 2
        baz = 3
        print magic_print()
        print magic_print('foo')
        print magic_print('foo','bar')
    

    Output:

    {'baz': 3, 'foo': 1, 'bar': 2}
    {'foo': 1}
    {'foo': 1, 'bar': 2}
    
    0 讨论(0)
提交回复
热议问题