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. 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
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
Why are you using eval in the first place? This isn't bash. Use setattr
and getattr for dynamic attribute access.
So you're saying you don't know the answer? The question wasn't
"should I use setattr?"
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/
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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);...
|
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 {
|
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";
| |
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...
|
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...
|
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |