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
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 13.04, Mint 11, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1088
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Discussions

Who is online

Users browsing this forum: Bing [Bot] and 1 guest