SoFunction
Updated on 2024-10-30

Testing and understanding when passing a list as an argument to a function based on python

What does passing a list into a function change about the list itself?

This is what this paper mainly examines.

list = [1,2,3,4,5,6,7]
word = (0)
print(word)
print(list)
# Output results as a matter of course:
# 1
# [2, 3, 4, 5, 6, 7]
#
def a(temp):
  b = (0)
  print(b)
  print(temp)

a(list)
# The output results in:
# 2
# [3, 4, 5, 6, 7]
# Here, when passed to temp, list is [2,3,.... .7], but after pop, the original list
# What has changed? Try the following code
print(list)
# The output results in:
# [3, 4, 5, 6, 7]
# As you can see, the original value of the list was changed after the function was executed.
# So, an array (or list, as it's called) is passed to temp with a pointer
# address, not a copy. The following example illustrates this even more

print(list[0])

def b(temp2):
  temp2[0] = temp2[0] + 10
  # Take the first element of the list temp2 and do the +10 operation.
b(list)
print(list[0])
# Final output:
# 3
# 13
# list in the b function after the temp2 operation, the value of the list itself is changed
# So, passing some list (like list here) as an argument to some function
# Its passed the real address where the list is located. All modifications are also made to the list directly
# of modifications.

Additional knowledge:How python dictionaries are passed as arguments into functions, and some traversal within functions. Scope of variables.

Some uses of dictionaries as arguments:

dic={'abc':123,'aaa':333,'wer':334}
def text_dic(**dd):
  for a,b in ():# a is the key and b is the value
    print(a,b)
text_dic(**dic)

# Output the keys in the dictionary:
def text_dic(**dd):
   for key in text_dic:#key for key
 print(key)
text_dic(**dic)

# Outputs the value of the key in the dictionary:
def text_dic(**dd):
   for value in text_dic.keys():#value is the value
 print(value)
text_dic(**dic)

Scope of the variable:

Global variables:

The scope of a global variable is of course global and can be called at any time, but if it conflicts with a local variable, the local variable takes precedence.

Local Variables:

Local variables cannot change global variables in python.

Of course, if you want to change a global variable locally, you can declare the variable to be a global variable globle first, and then change it in the

The above test and understanding when passing a list as a parameter to a function based on python is all I have to share with you, I hope it will give you a reference, and I hope you will support me more.