Simpler way to create dictionary of separate variables?

前端 未结 27 1921
名媛妹妹
名媛妹妹 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:37

    This is not possible in Python, which really doesn't have "variables". Python has names, and there can be more than one name for the same object.

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

    Well, I encountered the very same need a few days ago and had to get a variable's name which was pointing to the object itself.

    And why was it so necessary?

    In short I was building a plug-in for Maya. The core plug-in was built using C++ but the GUI is drawn through Python(as its not processor intensive). Since I, as yet, don't know how to return multiple values from the plug-in except the default MStatus, therefore to update a dictionary in Python I had to pass the the name of the variable, pointing to the object implementing the GUI and which contained the dictionary itself, to the plug-in and then use the MGlobal::executePythonCommand() to update the dictionary from the global scope of Maya.

    To do that what I did was something like:

    import time
    
    class foo(bar):
    
        def __init__(self):
            super(foo, self).__init__()
            self.time = time.time() #almost guaranteed to be unique on a single computer
    
        def name(self):
            g = globals()
            for x in g:
                if isinstance(g[x], type(self)):
                    if g[x].time == self.time:
                        return x
                        #or you could:
                        #return filter(None,[x if g[x].time == self.time else None for x in g if isinstance(g[x], type(self))])
                        #and return all keys pointing to object itself
    

    I know that it is not the perfect solution in in the globals many keys could be pointing to the same object e.g.:

    a = foo()
    b = a
    b.name()
    >>>b
    or
    >>>a
    

    and that the approach isn't thread-safe. Correct me if I am wrong.

    At least this approach solved my problem by getting the name of any variable in the global scope which pointed to the object itself and pass it over to the plug-in, as argument, for it use internally.

    I tried this on int (the primitive integer class) but the problem is that these primitive classes don't get bypassed (please correct the technical terminology used if its wrong). You could re-implement int and then do int = foo but a = 3 will never be an object of foo but of the primitive. To overcome that you have to a = foo(3) to get a.name() to work.

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

    With python-varname you can easily do it:

    pip install python-varname

    from varname import Wrapper
    
    foo = Wrapper(True)
    bar = Wrapper(False)
    
    your_dict = {val.name: val.value for val in (foo, bar)}
    
    print(your_dict)
    
    # {'foo': True, 'bar': False}
    

    Disclaimer: I'm the author of that python-varname library.

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