开发者

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

开发者 https://www.devze.com 2023-02-01 10:02 出处:网络
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 F

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=[]
开发者_如何学Python    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


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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消