时间:2020-11-04 11:24:17 | 栏目:Python代码 | 点击:次
java开发者在定义类中的方法时,不会关心方法的定义相对于调用语句的位置。
但是python中需要注意:
函数必须先定义、后调用(函数调用函数例外)。
如下为示例说明:
1、python函数的应用一般需要:先定义、后调用:
2、如果函数定义在调用之后,执行将报错:
3、函数中调用函数不受此限制:
4、但是语句对函数调用,必须在函数调用之后,包括直接调用的函数调用的其他函数也必须在调用语句之前,否则报错:
补充知识:Python的列表等变量的声明必须要在函数定义与函数调用之前
源代码
―?C这个代码作用是调用Linux应用终端操作来实现计算,并把计算结果取出来,组成相似矩阵,有五组值也就是有五组相似矩阵,这个是我做蛋白质结构预测用到的,自己写的,分享出来给大家,如有疑惑,欢迎评论区交流。
# 此代码主要为说明问题,后面我会贴上完整代码以及数据。 def cal_simi(n): file = os.popen('cd '+path[n-1]+';ls').readlines() RMSD_simi = [] TMscore_simi = [] MasSubscore_simi = [] GDTTSscore_simi = [] GDTHAscore_simi = [] for fileName1 in file: temp1 = [];temp2=[];temp3=[];temp4=[];temp5=[] for fileName2 in file: comm = 'cd /home/stern/Desktop/TMscore;' + 'sudo ./TMscore ' + path[n-1] + fileName1[:-1] +' '+ path[n-1] + fileName2[:-1] # storge the result of the command in data data = os.popen(comm).readlines() temp1.append(data[14][32:38]);temp2.append(data[16][14:20]);temp3.append(data[17][14:20]) temp4.append(data[18][14:20]);temp5.append(data[19][14:20]) RMSD_simi.append(temp1) TMscore_simi.append(temp2) MasSubscore_simi.append(temp3) GDTTSscore_simi.append(temp4) GDTHAscore_simi.append(temp5) p1 = np.median(RMSD_simi);p2 = np.median(TMscore_simi);p3 = np.median(MasSubscore_simi) p4 = np.median(GDTTSscore_simi);p5 = np.median(GDTHAscore_simi) for i in range(len(file)): RMSD_simi[i][i] = p1 TMscore_simi[i][i] = p2 MasSubscore_simi[i][i] = p3 GDTTSscore_simi[i][i] = p4 GDTHAscore_simi[i][i] = p5 return RMSD_simi,TMscore_simi,MasSubscore_simi,GDTTSscore_simi,GDTHAscore_simi Xn = ['1abv_','1af7_','1cqkA','1csp_','1dcjA','256bA'] path = ['/home/stern/Desktop/1abv_/','/home/stern/Desktop/1af7_/','/home/stern/Desktop/1cqkA/', '/home/stern/Desktop/1csp_/','/home/stern/Desktop/1dcjA/','/home/stern/Desktop/256bA/'] # fsadf RMSD_simi1 = [] TMscore_simi1 = [] MasSubscore_simi1 = [] GDTTSscore_simi1 = [] GDTHAscore_simi1 = [] RMSD_simi1,TMscore_simi1,MasSubscore_simi1,GDTTSscore_simi1,GDTHAscore_simi1=cal_simi(1) print RMSD_simi1[1][1] print TMscore_simi1[2][2] print MasSubscore_simi1[3][3] print GDTTSscore_simi1[4][4] print GDTHAscore_simi1[5][5]
运行结果
―?C运行结果,错误如下,如果把# fsadf后面的列表的声明提到函数定义的前面,则错误就没有了,列表的声明必须要在函数的调用之前,这个就更不用讲了,如果你不在函数调用之前的话,会提示列表不存在。。
Xn = ['1abv_','1af7_','1cqkA','1csp_','1dcjA','256bA'] File "<stdin>", line 30 Xn = ['1abv_','1af7_','1cqkA','1csp_','1dcjA','256bA'] ^ SyntaxError: invalid syntax >>> path = ['/home/stern/Desktop/1abv_/','/home/stern/Desktop/1af7_/','/home/stern/Desktop/1cqkA/', ... '/home/stern/Desktop/1csp_/','/home/stern/Desktop/1dcjA/','/home/stern/Desktop/256bA/']