Skip to content

Tag: variables

Pickle all variables

I”m looking for a way to pickle all the variables with their values in a sorted order. I’m trying the method below: Expected result: Append value of va to totalList , then vb’s value..and so on. And then dump the totalList to a pickle file. I’m getting result like this: I want to get r…

Way to call method depending on variable?

I already have a working, but in my oppinion not beautiful solution for a part of a long script. My script uses several similar methods, that differ too much to combine. However I came to a point where I want to call one of those methods depending on a given variable. The names of the methods are build up lik…

Elegant way to check if a nested key exists in a dict?

Is there are more readable way to check if a key buried in a dict exists without checking each level independently? Lets say I need to get this value in a object buried (example taken from Wikidata): To make sure that this does not end with a runtime error it is necessary to either check every level like so: …

Getting the name of a variable as a string

I already read How to get a function name as a string?. How can I do the same for a variable? As opposed to functions, Python variables do not have the __name__ attribute. In other words, if I have a variable such as: I am looking for a function/attribute, e.g. retrieve_name() in order to create a DataFrame i…