[Numpy-discussion] exec: bad practice?

Sebastien Binet seb.binet@gmail....
Tue Sep 15 06:07:56 CDT 2009


hi John,

> I have a bit of code where I create arrays with meaningful names via:
> 
> meat = ['beef','lamb','pork']
> cut = ['ribs','cutlets']
> 
> for m in meat:
>    for c in cut:
>          exec("consumed_%s_%s = np.zeros((numxgrid,numygrid,nummeasured))"
>  % (m,c))
> 
> Is this 'pythonic'? Or is it bad practice (and potentially slow) to use the
> 'exec' statement?
usage of the exec statement is usually frown upon and can be side stepped.
e.g:

for m in meat:
    for c in cut:
      locals()['consumed_%s_%s' % (m,c)] = some_array

hth,
sebastien.
-- 
#########################################
# Dr. Sebastien Binet
# Laboratoire de l'Accelerateur Lineaire
# Universite Paris-Sud XI
# Batiment 200
# 91898 Orsay
#########################################


More information about the NumPy-Discussion mailing list