Ниже я показываю 2 примера использования функции exe c () в Python 3.7, один в классе и один в необработанном коде. Только последний работает правильно. Может ли кто-нибудь объяснить мне возможные причины и предложить решения?
#!/usr/bin/env python
import pandas as pd
################################# exec() WITHIN A CLASS ###################################
class Test:
def __init__(self):
pass
def f(self):
pose, frame, min_complexE, min_lEfree, best_structvar, best_conf, complexE, ligandE_bound, proteinE_bound, \
min_lEfree = [None] * 10
table = pd.read_table("sample_scores.txt", delim_whitespace=True, skiprows=[0]) # 1st line is a comment
table_columns = table.columns
for i, row in table.iterrows():
# Variable assignemnt
for varname in ["pose", "frame", "min_complexE", "min_lEfree", "best_structvar", "best_conf",
"complexE", "ligandE_bound", "proteinE_bound", "min_lEfree", "Eint"]:
if varname in table_columns:
exec("%s = %s" % (varname, row[varname]), globals(), globals())
else:
exec("%s = None" % (varname), globals(), globals())
print("proteinE_bound within class =", proteinE_bound)
def caller1(self):
self.f()
def caller2(self):
self.caller1()
Test().caller2()
################################# exec() IN RAW CODE ###################################
pose, frame, min_complexE, min_lEfree, best_structvar, best_conf, complexE, ligandE_bound, proteinE_bound, \
min_lEfree = [None] * 10
table = pd.read_table("sample_scores.txt", delim_whitespace=True, skiprows=[0]) # 1st line is a comment
table_columns = table.columns
for i, row in table.iterrows():
# Variable assignemnt
for varname in ["pose", "frame", "min_complexE", "min_lEfree", "best_structvar", "best_conf",
"complexE", "ligandE_bound", "proteinE_bound", "min_lEfree", "Eint"]:
if varname in table_columns:
exec("%s = %s" % (varname, row[varname]), globals(), globals())
else:
exec("%s = None" % (varname), globals(), globals())
print("proteinE_bound as raw code =", proteinE_bound)
Содержимое файла "sample_scores.txt":
# Contains all results. For the best result for each compound please refer to file BEST_RESULTS.
molname Eint complexE ligandE_bound proteinE_bound stereoisomer ionstate tautomer pose frame
LEM00001847 -63.000496 -17406.593934 -84.868633 -17258.724804 1 1 1 1 571
LEM00001847 -62.412897 -17474.918135 -64.778724 -17347.726515 1 1 1 1 171
LEM00001847 -61.249384 -17423.452346 -82.875735 -17279.327226 1 1 1 1 531