1

I have the following eval() thing:

c = Customer()
eval("c.name = row.value('customer', '{c}')".format(c=column_name), { 'c': c, 'row': row})

When I try to run that, I get this:

Traceback (most recent call last):
 File "./import.py", line 19, in <module>
 c = Customer.save_from_row(row)
 File "/home/jason/projects/mcifdjango/mcif/models/customer.py", line 43, in save_from_row
 eval("c.name = row.value('customer', '{c}')".format(c=column_name), { 'c': c, 'row': row})
 File "<string>", line 1
 c.name = row.value('customer', 'name')
 ^
SyntaxError: invalid syntax

What am I doing wrong?

Edit: Because it looks like I didn't explain the context of my problem well enough, here's what I ended up doing, if anyone's curious:

@classmethod
def save_from_row(cls, row):
 c = cls()
 map(lambda column_name: setattr(c, column_name, row.value('customer', column_name)), c.distinguishing_column_names())
 return c.upsert()

Before I found out about setattr() I was separately setting several different attributes on c.

asked Feb 1, 2011 at 19:32
4
  • 1
    So... what's wrong with c.name = row.value('customer', column_name)? Why get eval() involved? Commented Feb 1, 2011 at 19:39
  • Because I have a whole list of different column names and properties of Customer that I need to assign. Commented Feb 1, 2011 at 19:42
  • Why not use getattr and setattr? Commented Feb 1, 2011 at 19:49
  • Because I didn't know about them. I tried setattr and it did exactly what I wanted. If you post setattr as an answer I'll accept it. Commented Feb 1, 2011 at 19:53

2 Answers 2

11

eval evaluates expressions. Assignment is a statement, not an expression.

And don't even get me started on how easily misused and - in 99.99% of all cases - utterly unnecesary eval is. Just refer to the numerous other eval questions, I bet each has at least one such rant in an answer or comment - so I'll save my breath and link to one I like. (That being said, exec works like eval for statements.)

answered Feb 1, 2011 at 19:36
Sign up to request clarification or add additional context in comments.

2 Comments

Any recommendation for what I might do instead of eval?
@Jason: Depends on what you need. In general, if you are emulating PHP's variable variables - i.e. associate values with strings - a dictionary will do work as well while preserving sanity (and be orders of magnitude faster while we're at it).
2

Wouldn't this do what you need?:

c = Customer()
name_cols = (('name', 'custname'), ('addr', 'cust_addr'))
for name, col in name_cols:
 setattr(c, name, row.value('customer', col))
answered Feb 1, 2011 at 19:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.