Eval Function related ?

A forum for general discussion of the Python programming language.

Eval Function related ?

Postby vijaykumbhani » Mon Sep 16, 2013 7:16 am

Examples:
Code: Select all
string = ' "name" == "name" '
print eval(string)


above code working

but

Examples:
Code: Select all
string = ' name == name '
print eval(string)


above code not working ?

any idea
without quotes in string
working properly
give me function or any library name work as well as eval
[b][i]With Regards,
Vijay Kumbhani[/i][/b]
vijaykumbhani
 
Posts: 33
Joined: Wed Aug 14, 2013 4:26 am
Location: Surat, Gujarat, India

Re: Eval Function related ?

Postby metulburr » Mon Sep 16, 2013 7:38 am

eval is essetially converting the string to code.

Code: Select all
' "name" == "name" '

becomes
Code: Select all
"name" == "name"


Code: Select all
' name == name '

becomes
Code: Select all
name == name


and unless you did not show the whole code, you should get a NameError with the second. It is checking if the variable name is equal to the variable name. Since you did not create a variable name, you should get a NameError. Whereas the first one is comparing the strings.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1473
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 3 guests