Code Yarns β€πŸ‘¨β€πŸ’»
Tech Blog ❖ Personal Blog

eval in Python

πŸ“… 2012-May-03 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ eval, python ⬩ πŸ“š Archive

eval in Python works like in many functional programming languages. It evaluates the input string as if it were a Python expression. Python’s eval can only handle expressions, so is pretty limited.

x = 1
y = eval( "x + 1" ) # 2

f = eval( "lambda x: x * x" )
g = f( 10 ) # 100

Tried with: Python 3.2


Β© 2022 Ashwin Nanjappa β€’ All writing under CC BY-SA license β€’ 🐘 @codeyarns@hachyderm.io β€’ πŸ“§