[Zope] exec?? issue
Chris Withers
chrisw@nipltd.com
Mon, 29 Jul 2002 08:48:51 +0100
Greg Conway wrote:
> Counter=0
> for GetDoc in context.DisplayCustomerSQL(SelectCID=TestCID):
> print "Customer name is %s %s" % (GetDoc.FirstName, GetDoc.LastName)
> for FieldCount in FieldName:
> ExecField = "GetDoc."+FieldName[Counter]
> print "value of field %s is %s" % (FieldCount, ExecField)
> Counter=Counter+1
>
> (yes I realise there are probably tidier ways to do this - especially the
> counter bit! - but hey I am learning!! albeit bad habits.... :)
What's FieldName?
> So, by the time the script above gets to the second-from-last line, the var
> ExecField contains something like GetDoc.Field1 or GetDoc.Field2.
>
> As you've probably guessed by now, I want to display the literal value of
> ExecField, instead than the word "ExecField" at this stage.... I'm not
> python programmer, but having scanned my copy of "learing Python"
> thoroughly, I reckon I'd use the "exec" command to do this.
I don't. exec is bad, never use exec. Can you spell 'security hole'? ;-)
> So, does anybody have any idea how I might solve my dilemma...??
you're specifically looking for:
getattr(object,variable_containing_attribute_name)
Here's a neater version of your code:
for GetDoc in context.DisplayCustomerSQL(SelectCID=TestCID):
print "Customer name is %s %s" % (GetDoc.FirstName, GetDoc.LastName)
for name in FieldName:
print "value of field %s is %s" % (name, getattr(GetDoc,name))
> (which, by the way, I've arrived at by giving up on trying to Mail Merge
> Zope with Word,
What were the problems there?
cheers,
Chris