There is always a way, though not every idea is always advised. For instance, the approach below is a bit hack-ish, but without more information about the structure of your program and what your actual needs are, it’s not really possible to suggest much better.
In [1]: a, b, c, e = 10, 20, 30, 50
In [2]: keys = ("a", "b", "c", "e")
In [3]: d = { k: globals().get(k) for k in keys }
In [4]: d
Out[4]: {'a': 10, 'b': 20, 'c': 30, 'e': 50}
Thanks. I want to dump a lot of variables (dictionaries, dataframes, etc, which are a, b, c, e in my question) by pickle, but I need to dump to multiple pkl files unless I create a new big dictionary to contain all variables. If I create a new big dictionary, then I just need to create one pickle file
What do you mean with “quicker”? Since they’re “dictionaries, dataframes, etc”, I doubt you’ll notice a speed difference between any methods (unless you do something artificially bad).
You can call pickle.dump(obj, file) multiple times with the same open file:
with file("mydump.pkl", "wb") as f:
pickle.dump(a, f)
pickle.dump(b, f)
pickle.dump(c, f)
pickle.dump(d, f)
But in any case you seem to need to know which objects you are pickling (i.e., their names). So you can just either do the pickling or make the dict by hand.
Is the more general problem that you somehow want to save and restore the state of your program?
You don’t need a dictionary for this. You can define a function to pickle all of them, and then use the method that Andrew described:
def pickle_all(outfile, *args):
with open(outfile, "w+b") as f:
for obj in args:
pickle.dump(obj, f)
Alternatively, you could also define this by pickling them all together, as one big tuple:
def pickle_all(outfile, *args):
with open(outfile, "w+b") as f:
pickle.dump(args, f)
Both of those can be called on whatever number of objects you have:
pickle_all(x, y)
pickle_all(a, b, c) # takes any number of arguments
If for whatever reason you also want to pickle the original variables names of your objects, then you could do so by using **kwargs (keyword arguments) which is a dictionary constructed when you’re calling the function:
def pickle_all(outfile, **kwargs):
with open(outfile, "w+b") as f:
pickle.dump(kwargs, outfile)
To be called as
pickle_all(outfile, a=a, b=b, c=c, df=df) # etc
Or – if you already have some dictionary, you could then also call:
pickle_all(outfile, **my_objects_dict)
If you’re wondering how the magic of **kwargs works, then you can quickly get a sense of this by running: