What does the def line do in python

What does the second in the last line do? This doesn't seem to be a variable assignment, print, or anything else, but I tried to break the code in various ways and the doublelist(somelist) seems necessary, but I don't know why.

 def doubleList(list): i=0 while i<len(list): list[i]=list[i]*2 i=i+1 someList=[34,72,96] doubleList(someList) print someList 
+4
source share
3 answers

Functions can modify mutable arguments passed to them. A list (poorly named), called a "list", has (using a non-idiomatic style) each of its elements, multiplied by two, in place. For instance:

 >>> def inplace(seq): ... seq[0] = 5 ... >>> a = [1,2,3] >>> print a [1, 2, 3] >>> inplace(a) >>> a [5, 2, 3] 
+4
source

Statement

 def doublieList(list): 

and the following indented lines just create a function

The next line prints this function to someList

+3
source

Since @DSM already answered this question, I will put a more pythonic version of this function:

  def doubleList(something_other_than_list): for i in range(len(something_other_than_list)): something_other_than_list[i] *= 2 
+1
source

Source: https://habr.com/ru/post/1396067/


All Articles