what is the difference between “eval” and “int”

后端 未结 2 450
谎友^
谎友^ 2021-01-18 11:20

Earlier I heard that eval(input(a)) will convert a string automatically to int, but if i code

age = eval(input(\"enter age\"))

2条回答
  •  一个人的身影
    2021-01-18 11:52

    eval() is used to verify an expression. On number is considered an expression, except octal numbers (numbers that start with 0). int() handles string to integer conversion. There are many reasons why you should avoid using eval(). Just keep in mind:

    • Python 2.x

      x = raw_input('Enter number here: ') 
      
    • Python 3.x

      x = input('Enter number here: ') 
      
    • Python 2.x
      Security risk:

      x = input('Enter number here: ') 
      
    • Python 3.x
      Security risk:

      x = eval(input('Enter number here: ')) 
      

    Also, keep in mind that eval() has the potential to run code, which could cause a huge security risk. I suggest not using it unless you clearly know what you're doing or it could compromise your application.

提交回复
热议问题