![]() |
How to avoid exec(), globals(), locals(), eval() - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to avoid exec(), globals(), locals(), eval() (/thread-33377.html) Pages:
1
2
|
RE: How to avoid exec(), globals(), locals(), eval() - snippsat - Apr-21-2021 (Apr-21-2021, 03:23 PM)paul18fr Wrote: I'm doing my best to have a good practiceThen can play a song at right moment The PEP 8 Song🎵 In this post an your other post your code look messy when look at it for having good practice in Python. So i rewrite it would look like this and code works. import numpy as np # Make random numpy array mat = np.random.random((10,1)) mat2 = np.random.random((10,1)) mat3 = np.random.random((10,1)) my_dict = {"new_dict": {'mat1': mat, 'mat2': mat2}} # dict to add basic_dict = {'mat': mat} new_key = 'new_key' if not new_key in my_dict: print(f"{new_key} is not in my_dictList -> added") my_dict.update({new_key: {}, }) my_dict.update(basic_dict) else: print(f"{new_key} is in my_dict") # Test my_dict get_mat2 = my_dict['new_dict']['mat2'] print(get_mat2) print('-' * 25) print(my_dict)
|