How to get the substitution target variable name when creating an instance

I am studying the open source programming language used by Python.

In Python, I want to get the name of the variable to which the generated instance is assigned as a string.
Please let me know if there are ways or alternatives to achieve this.

Code example

I'll show you the code I want to implement

# The current code looks like this
class ManualVariableNamed:
    def __init __ (self, name):
        self.variable_name = name # I want to store the variable name string here
def main ():
    x = ManualVariableNamed (name = 'x') # Giving a string like this feels redundant
    print (x.variable_name) # =>'x'

# The following code example
# Consider a class that can automatically get the variable name when creating an instance
class GetSelfVariableNameClass:
    def __init __ (self):
        self.variable_name = get_variable_name (self) # For example
def main ():
    y = GetSelfVariableNameClass ()
    print (y.variable_name) # =>'y'

A list of variable names in the current scope can be obtained withglobals ()orlocals ().
We thought about using this to look for a variable that matchesid (self).
However, (1) there is no way to find the variable before substitution evaluation from (2) a different scope.
I don't want to explicitly give the current scope in the initializer argument.

In this post, Qiita: How to get as a string is introduced.
But what I wanted to do was not solved.

You can talk at the spec level, so please let me know if you have any hints.
If this is not possible, it would be helpful if you could tell me the grounds. Thank you in advance.

  • Answer # 1

    Isn't it intuitive? I think. The behavior is not expected at all in the language specification.

    I feel like I shouldn't do it if there was a way to force it (such as passing the caller's syntax information).

    You may come up with the idea of ​​passinglocals ()(document) and rewriting it as the next best solution, but this is not possible because it cannot be rewritten.

    If you create your own dictionary instead of the namespace, you can do the equivalent of ↑. It's like this. It can be done, but is there any advantage ... It will not receive support such as static analysis tools.

    class GetSelfVariableNameClass:
        # variable_name = None # The class variable is probably unintended
        def __init __ (self, name, names):
            self.variable_name = name
            names [name] = self
    def main ():
        names = dict ()
        GetSelfVariableNameClass ("y", names)
        # We will write names ["y"] instead of writing the local variable y
        print (names ["y"]. variable_name) # =>'y'

  • Answer # 2


    In Python, I want to get the name of the variable to which the generated instance is assigned as a string.

    In the first place, I don't know why this behavior is necessary.

    Even if you can do it, an object whose behavior changes just by changing the name of a local variable istoo stupid(because it is tightly coupled to the calling environment ) And since local variables are just local and are not used for external I/F, there should be no use for them from the called code.

  • Answer # 3

    Variable dictionary list can be taken with vars (), isn't that the case?
    The argument of the vars function can specify the object for which you want to take a variable dictionary.
    If no argument is specified, locals () will show the local variable dictionary.
    Besides, globals dictionary can be seen with globals ().・

    class SampleClass:
        def __init __ (self):
            self.x = 123
    def main ():
        y = SampleClass ()
        print (vars (y))
        print (vars ())
    if __name__ == '__main__':
        main ()


    {'x': 123}
    {'y':<__ main __. SampleClass object at 0x6ffffd106a0>}

  • Answer # 4

    There are no local variables yet when instantiated.
    For example
    i = j = obj ()
    If so, what do you store?
    return obj ()

    That's not possible.

Related articles