简体   繁体   中英

What is the efficient way to write code where argument with same value is passed to a function that is called multiple times ? (python)

def filtered_imagesIds(imageParam,serverId)
   ###code to get subset of data provided using serverId

def oldest_image(filteredImageParam)
   ### code to find oldest image  
mainfunc()
  completeImageSet = (### code to get whole set of ImageIds)
  for server in serverList:
      filteredImageSet = fitered_ImageIds(completeImageSet,server)
      oldestImage = oldest_image(filteredImageSet)
      oldImageList.append(oldestImage)
  print oldImageList

This is a rough skeleton of my code. As you can see I'm trying to get oldest image for a server from filtered set of images obtained from whole set of images.

I'm new to python and I'm not sure how efficient it is to pass an argument with same value( completeImageSet ) to a function( filtered_ImageIds ) that will be called multiple times.

Will an object be created each time the function is called? If so would it be more efficient to have something like a global variable?

As Adam Smith mentioned, arguments are passed by object assignment, so the value of completeImageSet should not change as you iterate it over the loop.

You can also keep things simple using list comprehension instead;

oldImageList = [oldest_image(filtered_ImageIds(completeImageSet, server)) for server in serverList]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM