0

Before Stating anything, I've read about this and this. But I'm still unclear of the fact that where eval and repr is actually used/applied to? Real-life applications

Community
  • 1
  • 1
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • 10
    `eval` is used any time you want to create a security vulnerability in your software – wim Jan 02 '14 at 16:20
  • So, is it like negative testing? – Praful Bagai Jan 02 '14 at 16:20
  • @wim nice one :) user1162512 you can read this [post](http://stackoverflow.com/q/15197673/1982962) regarding `eval()` – Kobi K Jan 02 '14 at 16:21
  • 1
    While this is indeed a great question, separately, both `eval` and `repr` have been explained many, many times on this site, there are so many duplicates of this question it turns my head. Please search the site next time as you did for understanding `repr` by your own admission. I really don't see why you asked this question. – Inbar Rose Jan 02 '14 at 16:24
  • @InbarRose - Didnt you saw I already provide the best threads on SO related to same(I've read many), but I didnt really saw the real-life application of either one. Secondly, the question that you tagged as duplicate with, my question is completely different from it. – Praful Bagai Jan 02 '14 at 16:33
  • @user1162512 That is your opinion. If you want to ask a targeted question along the lines of "what are some real-life applications of eval?" you should know it is not a good fit for this sites format. Secondly, what kind of answers do you expect to get from such an opinionated function? It is like asking "what are real-life applications of print?" You will get anything as an answer, none of it will actually be helpful to anyone. Consult [ASK] if you want. – Inbar Rose Jan 02 '14 at 16:42

1 Answers1

1

repr is used all the time under the hood, for instance each time you format a container, most of the time when you print something etc.

>>> class A:
...     def __repr__(self):
...             print('calling repr')
...             return('<object of class A>')
... 
>>> print(A())
calling repr
<object of class A>
>>> a = [A() for _ in range (3)]
>>> '{}'.format(a)
calling repr
calling repr
calling repr

eval... I don't know. I even think that those who claim it to be necessary, might be wrong.

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
  • `eval` alone is not necessary, as it can be easily replaced by other functions. The ability of loading arbitrary Python code (which is the source of all the problems with `eval`, and shared by the functions that can be used to re-implement `eval`), however, *is* necessary sometimes. –  Jan 02 '14 at 16:23
  • @delnan For those instances, I personally prefer `import` or `__import__` over `eval`. I don't know, maybe it is just a long time pampered grudge of mine against `eval` and its brethren. – Hyperboreus Jan 02 '14 at 16:29
  • Without further qualification, that indeed seems like an irrational phobia. I would count `__import__` among the bretheren, it's essentially `exec` combined with `find_module_path` and `open`. If you have a string, and dump it to a file so you can import it dynamically, you haven't gained *anything* (but increased complexity by adding file I/O to your metaprogramming). Of course, if you just want to dynamically load a module that's already in a file, the import machinery is just the right thing. –  Jan 02 '14 at 16:31