473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

properties vs. eval()

Given this class:

class C(object):
def set_x(self, x):
self._x = x

def get_x(self):
return self._x

x = property(get_x, set_x)
This use of compile() and eval() works as I expected it to:

c = C()
c.x = 5000
n = '\'five thousand\''
code = compile('c.x = ' + n, '<input>', 'exec')
print 'before ', c.x
eval(code)
print 'after ', c.x

But this, using eval() without compile(), does not:

c = C()
c.x = 5000
n = '\'five thousand\''
print 'before ', c.x
eval('c.x = ' + n)
print 'after ', c.x

It gives:

before 5000
Traceback (most recent call last):
File "./r.py", line 16, in ?
eval('c.x = ' + n)
File "<string>", line 1
c.x = 'five thousand'
^
SyntaxError: invalid syntax

Could someone please explain just what is going on here, and whether it
is possible to dispense with the compile step and use eval() alone
while setting a property?

Thanks.

Jul 19 '05 #1
6 1608
Bob Rogers wrote:
Given this class:

class C(object):
def set_x(self, x):
self._x = x

def get_x(self):
return self._x

x = property(get_x, set_x)
This use of compile() and eval() works as I expected it to:

c = C()
c.x = 5000
n = '\'five thousand\''
code = compile('c.x = ' + n, '<input>', 'exec')
print 'before ', c.x
eval(code)
print 'after ', c.x
I believe it is an implementation accident that this works.

But this, using eval() without compile(), does not:

c = C()
c.x = 5000
n = '\'five thousand\''
print 'before ', c.x
eval('c.x = ' + n)
print 'after ', c.x

It gives:

before 5000
Traceback (most recent call last):
File "./r.py", line 16, in ?
eval('c.x = ' + n)
File "<string>", line 1
c.x = 'five thousand'
^
SyntaxError: invalid syntax

Could someone please explain just what is going on here, and whether it
is possible to dispense with the compile step and use eval() alone
while setting a property?


Use

eval(s)

to evaluate an expression and

exec s

to execute a statement.

Peter

Jul 19 '05 #2
Peter Otten wrote:
Use

eval(s)

to evaluate an expression and

exec s

to execute a statement.


I never thought of using compile() to get around this before. Now I can
finally use print in my lambda functions! Just think of the horrible
code I could write:

f = lambda x: eval(compile("print %s" % x, "<string>", "exec"))
--
Michael Hoffman
Jul 19 '05 #3
Why are you using eval in the first place? This isn't bash. Use setattr
and getattr for dynamic attribute access.

Jul 19 '05 #4
So you're saying you don't know the answer? The question wasn't
"should I use setattr?"

Jul 19 '05 #5
Bob Rogers wrote:
So you're saying you don't know the answer? The question wasn't
"should I use setattr?"

No, the "*question*" was (paraphrasing slightly) "is [it] possible to
dispense with the compile step and use eval() alone while setting a
property" the *answer* was "you should use setattr".

If you don't see that this implies that using eval() is not a good idea
until other resources have been exhausted, then you need to understand:
"using eval() is not a good idea until other resources have been exhausted".

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/

Jul 19 '05 #6
Bob Rogers wrote:
So you're saying you don't know the answer? The question wasn't
"should I use setattr?"


If what you're doing is wrong and backwards then it doesn't matter what
the question is. Best practices are best practices for a reason.
There's no reason to use eval to do what you want. The constructs
eval/exec/compile all have their purposes (for example: a remote python
shell) but they weren't intended to be broken replacements for setattr.

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Arno R | last post by:
Hi all, I have lots of properties in a class. (clsTest) Properties are created like (simplified ...) Public ItemA as string Public ItemB as string Public ItemC as string .... Public...
2
by: MattBell | last post by:
I have what I'm hoping doesn't turn into a very big problem. We have a set of value objects which all inherit from a base value object which contains functionality common to all of the objects. We...
2
MsNova
by: MsNova | last post by:
I am getting an error ‘pic1 has no properties in this code’, which is supposed to float a gif file (alien.gif in this case) around the screen. The code works fine in IE, but in Netscape and Firefox...
1
by: arturofonseca | last post by:
I have an event where attendants can register for online. This is 2 day event. I want to be able to disable the same lab on day2 when it has been selected on day1 (so attendants don't select the same...
6
by: =?ISO-8859-1?Q?Ignacio_Burgue=F1o?= | last post by:
Hi everyone. I'm dealing with some javascript code which uses eval to access properties of an object. For instance, I have the following: var events = {}; events.flatUsers = {};...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.