473,805 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1631
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("p rint %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
1775
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 CountA as integer
2
1188
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 have implemented some custom DataSourceObjects which return lists of these value objects. The problem I'm running into is I can't bind to any of the common properties of the ancestor object. Nor can I even eval against them. Is there someway...
2
1335
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 I get the following error: Error: pic1 has no properties Line: 66 Which is this line: pic1.moveTo((x+pageXOffset),(y+pageYOffset)) I thought pic1 was defined with this line in the header: var pic1=eval('document.'+_all+'pic1'+_style);...
1
1239
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 lab on two different days). This is the function I am using... function DisableOther(a){ var myD = a.name.substring(4, 5); var myS = a.name.substring(6, 7); var othD; if (myD == 1){ othD = '2'; } else {
6
1237
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 = {}; events.flatUsers.Clone = "I'm the Clone property"; events.flatUsers.Edit = "I'm the Edit property";
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10359
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.