Is Using eval In Python A Bad Practice?

Answer

Yes, using eval is a bad practice. Just to name a few reasons:

  1. There is almost always a better way to do it
  2. Very dangerous and insecure
  3. Makes debugging difficult
  4. Slow

In your case you can use setattr instead:

classSong:"""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)

All python Questions

Ask your interview questions on python

Write Your comment or Questions if you want the answers on python from python Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---