+ 1
Why is using a keyword like exec() or eval() in python not good coding?
When I get a chance I enjoy using exec() for for to create dynamic namedtuples or maybe a widget in PyQt. But why is this not good coding? See an example below: class App(QMainWidget): def __init__(self): super().__init__(self) self.grid=QGridLayout() txt = ('clear','next') for n,t in enumerate(txt): exec(f'b{n}=QPushButton(t)') exec(f'self.grid.addWidget(b{n},2,{n+1})') exec('b{n}.clicked.connect(self.{t})') def clear(self): pass def next(self): pass obviously, above is incomplete code, but you get the picture
1 Odpowiedź
+ 1
Your code should be PEP 8 complaint.
Also eval and exec are often marked as undesirable because of the complexity they induce. When you see a eval call you often don't know what's really going on behind it, because it acts on data that's usually in a variable. This makes code harder to read.
This seems to explain why better than I can.
http://stupidpythonideas.blogspot.com/2013/05/why-evalexec-is-bad.html