Asked By – Nikwin
I use the following class to easily store data of my songs.
class Song: """The class to store the details of each song""" attsToStore=('Name', 'Artist', 'Album', 'Genre', 'Location') def __init__(self): for att in self.attsToStore: exec 'self.%s=None'%(att.lower()) in locals() def setDetail(self, key, val): if key in self.attsToStore: exec 'self.%s=val'%(key.lower()) in locals()
I feel that this is just much more extensible than writing out an
if/else block. However, I have heard that
eval is unsafe. Is it? What is the risk? How can I solve the underlying problem in my class (setting attributes of
self dynamically) without incurring that risk?
Now we will see solution for issue: Why is using ‘eval’ a bad practice?
eval is a bad practice. Just to name a few reasons:
- There is almost always a better way to do it
- Very dangerous and insecure
- Makes debugging difficult
In your case you can use setattr instead:
class Song: """The class to store the details of each song""" attsToStore=('Name', 'Artist', 'Album', 'Genre', 'Location') def __init__(self): for att in self.attsToStore: setattr(self, att.lower(), None) def setDetail(self, key, val): if key in self.attsToStore: setattr(self, key.lower(), val)
There are some cases where you have to use
exec. But they are rare. Using
eval in your case is a bad practice for sure. I’m emphasizing on bad practice because
exec are frequently used in the wrong place.
Replying to the comments:
It looks like some disagree that
eval is ‘very dangerous and insecure’ in the OP case. That might be true for this specific case but not in general. The question was general and the reasons I listed are true for the general case as well.