Trying to understand which is better in python creating variables or using expressions

混江龙づ霸主 提交于 2019-12-24 06:44:09

问题


One of the practices I have gotten into in Python from the beginning is to reduce the number of variables I create as compared to the number I would create when trying to do the same thing in SAS or Fortran

for example here is some code I wrote tonight:

def idMissingFilings(dEFilings,indexFilings):
    inBoth=set(indexFilings.keys()).intersection(dEFilings.keys())
    missingFromDE=[]
    for each in inBoth:
        if len(dEFilings[each])<len(indexFilings[each]):

            dEtemp=[]
            for filing in dEFilings[each]:
                #dateText=filing.split("\\")[-1].split('-')[0]
                #year=dateText[0:5]
                #month=dateText[5:7]
                #day=dateText[7:]
                #dETemp.append(year+"-"+month+"-"+day+"-"+filing[-2:])    
            dEtemp.append(filing.split('\\')[-1].split('-')[0][1:5]+"-"+filing.split('\\')[-1].split('-')[0][5:7]+"-"+filing.split('\\')[-1].split('-')[0][7:]+"-"+filing[-2:])
            indexTemp=[]
            for infiling in indexFilings[each]:
                indexTemp.append(infiling.split('|')[3]+"-"+infiling[-6:-4])
            tempMissing=set(indexTemp).difference(dEtemp)
            for infiling in indexFilings[each]:
                if infiling.split('|')[3]+"-"+infiling[-6:-4] in tempMissing:
                    missingFromDE.append(infiling)
    return missingFromDE

Now I split one of the strings I am processing 4 times in the line dEtemp.append(blah blah blah)

filing.split('\\')

Historically in Fortran or SAS if I were to attempt the same I would have 'sliced' my string once and assigned a variable to each part of the string that I was going to use in this expression.

I am constantly forcing myself to use expressions instead of first resolving to a value and using the value. The only reason I do this is that I am learning by mimicking other people's code but it has been in the back of my mind to ask this question - where can I find a cogent discussion of why one is better than the other

The code compares a set of documents on a drive and a source list of those documents and checks to see whether all of those from the source are represented on the drive

Okay the commented section is much easier to read and how I decided to respond to nosklos answer


回答1:


Yeah, it is not better to put everything in the expression. Please use variables.

Using variables is not only better because you will do the operation only once and save the value for multiple uses. The main reason is that code becomes more readable that way. If you name the variable right, it doubles as free implicit documentation!




回答2:


Use more variables. Python is known for its readability; taking away that feature is called not "Pythonic" (See https://docs.python-guide.org/writing/style/). Code that is more readable will be easier for others to understand, and easier to understand yourself later.



来源:https://stackoverflow.com/questions/4516118/trying-to-understand-which-is-better-in-python-creating-variables-or-using-expre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!