Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is an usage scenario where you might need this. I'm not implying there are not better ways or achieving the same functionality.</p> <p>This would be useful in order to 'dump' an arbitrary list of dictionaries in case of error, in debug modes and other similar situations.</p> <p>What would be needed, is the reverse of the <code>eval()</code> function:</p> <pre><code>get_indentifier_name_missing_function() </code></pre> <p>which would take an identifier name ('variable','dictionary',etc) as an argument, and return a string containing the identifier’s name.</p> <hr> <p>Consider the following current state of affairs:</p> <pre><code>random_function(argument_data) </code></pre> <p>If one is passing an identifier name ('function','variable','dictionary',etc) <code>argument_data</code> to a <code>random_function()</code> (another identifier name), one actually passes an identifier (e.g.: <code>&lt;argument_data object at 0xb1ce10&gt;</code>) to another identifier (e.g.: <code>&lt;function random_function at 0xafff78&gt;</code>):</p> <pre><code>&lt;function random_function at 0xafff78&gt;(&lt;argument_data object at 0xb1ce10&gt;) </code></pre> <p>From my understanding, only the memory address is passed to the function:</p> <pre><code>&lt;function at 0xafff78&gt;(&lt;object at 0xb1ce10&gt;) </code></pre> <p>Therefore, one would need to pass a string as an argument to <code>random_function()</code> in order for that function to have the argument's identifier name:</p> <pre><code>random_function('argument_data') </code></pre> <p>Inside the random_function()</p> <pre><code>def random_function(first_argument): </code></pre> <p>, one would use the already supplied string <code>'argument_data'</code> to:</p> <ol> <li>serve as an 'identifier name' (to display, log, string split/concat, whatever)</li> <li><p>feed the <code>eval()</code> function in order to get a reference to the actual identifier, and therefore, a reference to the real data:</p> <pre><code>print("Currently working on", first_argument) some_internal_var = eval(first_argument) print("here comes the data: " + str(some_internal_var)) </code></pre></li> </ol> <p>Unfortunately, this doesn't work in all cases. It only works if the <code>random_function()</code> can resolve the <code>'argument_data'</code> string to an actual identifier. I.e. If <code>argument_data</code> identifier name is available in the <code>random_function()</code>'s namespace.</p> <p>This isn't always the case:</p> <pre><code># main1.py import some_module1 argument_data = 'my data' some_module1.random_function('argument_data') # some_module1.py def random_function(first_argument): print("Currently working on", first_argument) some_internal_var = eval(first_argument) print("here comes the data: " + str(some_internal_var)) ###### </code></pre> <p>Expected results would be:</p> <pre><code>Currently working on: argument_data here comes the data: my data </code></pre> <p>Because <code>argument_data</code> identifier name is not available in the <code>random_function()</code>'s namespace, this would yield instead:</p> <pre><code>Currently working on argument_data Traceback (most recent call last): File "~/main1.py", line 6, in &lt;module&gt; some_module1.random_function('argument_data') File "~/some_module1.py", line 4, in random_function some_internal_var = eval(first_argument) File "&lt;string&gt;", line 1, in &lt;module&gt; NameError: name 'argument_data' is not defined </code></pre> <hr> <p>Now, consider the hypotetical usage of a <code>get_indentifier_name_missing_function()</code> which would behave as described above.</p> <p>Here's a dummy Python 3.0 code: .</p> <pre><code># main2.py import some_module2 some_dictionary_1 = { 'definition_1':'text_1', 'definition_2':'text_2', 'etc':'etc.' } some_other_dictionary_2 = { 'key_3':'value_3', 'key_4':'value_4', 'etc':'etc.' } # # more such stuff # some_other_dictionary_n = { 'random_n':'random_n', 'etc':'etc.' } for each_one_of_my_dictionaries in ( some_dictionary_1, some_other_dictionary_2, ..., some_other_dictionary_n ): some_module2.some_function(each_one_of_my_dictionaries) # some_module2.py def some_function(a_dictionary_object): for _key, _value in a_dictionary_object.items(): print( get_indentifier_name_missing_function(a_dictionary_object) + " " + str(_key) + " = " + str(_value) ) ###### </code></pre> <p>Expected results would be:</p> <pre><code>some_dictionary_1 definition_1 = text_1 some_dictionary_1 definition_2 = text_2 some_dictionary_1 etc = etc. some_other_dictionary_2 key_3 = value_3 some_other_dictionary_2 key_4 = value_4 some_other_dictionary_2 etc = etc. ...... ...... ...... some_other_dictionary_n random_n = random_n some_other_dictionary_n etc = etc. </code></pre> <p>Unfortunately, <code>get_indentifier_name_missing_function()</code> would not see the 'original' identifier names (<code>some_dictionary_</code>,<code>some_other_dictionary_2</code>,<code>some_other_dictionary_n</code>). It would only see the <code>a_dictionary_object</code> identifier name.</p> <p>Therefore the real result would rather be:</p> <pre><code>a_dictionary_object definition_1 = text_1 a_dictionary_object definition_2 = text_2 a_dictionary_object etc = etc. a_dictionary_object key_3 = value_3 a_dictionary_object key_4 = value_4 a_dictionary_object etc = etc. ...... ...... ...... a_dictionary_object random_n = random_n a_dictionary_object etc = etc. </code></pre> <p>So, the reverse of the <code>eval()</code> function won't be that useful in this case.</p> <hr> <p>Currently, one would need to do this:</p> <pre><code># main2.py same as above, except: for each_one_of_my_dictionaries_names in ( 'some_dictionary_1', 'some_other_dictionary_2', '...', 'some_other_dictionary_n' ): some_module2.some_function( { each_one_of_my_dictionaries_names : eval(each_one_of_my_dictionaries_names) } ) # some_module2.py def some_function(a_dictionary_name_object_container): for _dictionary_name, _dictionary_object in a_dictionary_name_object_container.items(): for _key, _value in _dictionary_object.items(): print( str(_dictionary_name) + " " + str(_key) + " = " + str(_value) ) ###### </code></pre> <hr> <h2>In conclusion:</h2> <ul> <li>Python passes only memory addresses as arguments to functions.</li> <li>Strings representing the name of an identifier, can only be referenced back to the actual identifier by the <code>eval()</code> function if the name identifier is available in the current namespace.</li> <li>A hypothetical reverse of the <code>eval()</code> function, would not be useful in cases where the identifier name is not 'seen' directly by the calling code. E.g. inside any called function.</li> <li>Currently one needs to pass to a function: <ol> <li>the string representing the identifier name</li> <li>the actual identifier (memory address)</li> </ol></li> </ul> <p>This can be achieved by passing both the <code>'string'</code> and <code>eval('string')</code> to the called function at the same time. I think this is the most 'general' way of solving this egg-chicken problem across arbitrary functions, modules, namespaces, without using corner-case solutions. The only downside is the use of the <code>eval()</code> function which may easily lead to unsecured code. Care must be taken to not feed the <code>eval()</code> function with just about anything, especially unfiltered external-input data.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload