|
|
 |
 |
 |
 |
Python Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
problem with eval while using PythonCard
Hi its my first post. I have a problem, I want to user eval() function in a for loop to set labels to staticText so i done something like this: dzien=self.components.Calendar.GetDate().GetDay() for i in range(1,8): act=dzien+i -1 eval( 'self.components.d' + str(i) + '.text = "'+ str(act) + '"') but there is a problem, the program returned error: File "/home/lipton/Projekty/kami-organizer/grafik.py", line 27, in __init__ eval( 'self.components.d' + str(i) + '.text = "'+ str(act) + '"') File "<string>", line 1 self.components.d1.text = "25" SyntaxError: invalid syntax dont know what is wrong, becouse when i copy and paste in to code : self.components.d1.text = "25" it work great, but when Im trying to do it using eval() it give me a SyntaxError Please help. ps. sorry for my english ;)
On May 25, 3:33 pm, "Michal Lipinski" <michal.lipin@gmail.com> wrote:
> Hi > its my first post. I have a problem, I want to user eval() function in > a for loop to set labels to staticText so i done something like this: > dzien=self.components.Calendar.GetDate().GetDay() > for i in range(1,8): > act=dzien+i -1 > eval( 'self.components.d' + str(i) + '.text = "'+ str(act) + '"') > but there is a problem, the program returned error: > File "/home/lipton/Projekty/kami-organizer/grafik.py", line 27, in __init__ > eval( 'self.components.d' + str(i) + '.text = "'+ str(act) + '"') > File "<string>", line 1 > self.components.d1.text = "25" > SyntaxError: invalid syntax > dont know what is wrong, becouse when i copy and paste in to code : > self.components.d1.text = "25" > it work great, but when Im trying to do it using eval() it give me a SyntaxError > Please help. > ps. sorry for my english ;)
How about something like this: ############ #make object: class S(object):pass class X(object):pass class Y(object):pass s = S() s.components = X() s.components.d1 = Y() s.components.d1.text = "hello world" ########### obj = getattr(s.components, "d" + str(1)) obj.text = "goodybe" print s.components.d1.text
Here's a complete example: ################### #create object 's': class S(object):pass class X(object):pass class Y(object):pass s = S() s.components = X() s.components.d1 = Y() s.components.d2 = Y() s.components.d3 = Y() ###################### ###################### set some initial values: for i in range(1, 4): obj = getattr(s.components, "d" + str(i)) obj.text = "hello world" ##################### ##################### #change the values: for i in range(1, 4): getattr(s.components, "d" + str(i)).text = "goodybe" ##################### ##################### #print out the new values: for i in range(1, 4): print getattr(s.components, "d" + str(i)).text #####################
now it's working just fine. but still I dont know why eval dont work ? and thx for help 25 May 2007 15:05:03 -0700, 7stud <bbxx789_0@yahoo.com>:
> Here's a complete example: > ################### > #create object 's': > class S(object):pass > class X(object):pass > class Y(object):pass > s = S() > s.components = X() > s.components.d1 = Y() > s.components.d2 = Y() > s.components.d3 = Y() > ###################### > ###################### > set some initial values: > for i in range(1, 4): > obj = getattr(s.components, "d" + str(i)) > obj.text = "hello world" > ##################### > ##################### > #change the values: > for i in range(1, 4): > getattr(s.components, "d" + str(i)).text = "goodybe" > ##################### > ##################### > #print out the new values: > for i in range(1, 4): > print getattr(s.components, "d" + str(i)).text > ##################### > -- > http://mail.python.org/mailman/listinfo/python-list
-- Pozdrawiam Micha Lipiski http://lipton.kom.pl
On May 25, 4:43 pm, "Michal Lipinski" <michal.lipin@gmail.com> wrote:
> now it's working just fine. but still I dont know why eval dont work ? > and thx for help > 25 May 2007 15:05:03 -0700, 7stud <bbxx789_0@yahoo.com>: > > Here's a complete example: > > ################### > > #create object 's': > > class S(object):pass > > class X(object):pass > > class Y(object):pass > > s = S() > > s.components = X() > > s.components.d1 = Y() > > s.components.d2 = Y() > > s.components.d3 = Y() > > ###################### > > ###################### > > set some initial values: > > for i in range(1, 4): > > obj = getattr(s.components, "d" + str(i)) > > obj.text = "hello world" > > ##################### > > ##################### > > #change the values: > > for i in range(1, 4): > > getattr(s.components, "d" + str(i)).text = "goodybe" > > ##################### > > ##################### > > #print out the new values: > > for i in range(1, 4): > > print getattr(s.components, "d" + str(i)).text > > ##################### > > -- > >http://mail.python.org/mailman/listinfo/python-list > -- > Pozdrawiam > Micha Lipiski > http://lipton.kom.pl
1) Don't use eval() 2) Don't use eval() 3) eval() only works on python "expressions". In python, an assignment statement is not an expression.
|
 |
 |
 |
 |
|