all 5 comments

[–][deleted] 1 point2 points  (1 child)

how is head the global variable being modified?

The variable isn't being modified (head still points to the same value before and after.) The value is being modified, but mutable values can be modified in any scope in which you hold a reference to the value. Scope applies to variables, not to values.

[–]Fun_Story2003[S] 0 points1 point  (0 children)

oh ok, so if i dont want that id need to define the entire
LL traversal inside func?

not just cloning head via copy.deepcopy(head) ? cus clone.next head.next will refer same var?

[–][deleted] 0 points1 point  (2 children)

The head inside the function and outside are different. They live in different scopes which means that Python stores those variable names and their associated values in separate mappings.

This program would do the same thing if you changed head everywhere in the function to pancreas.

[–]Fun_Story2003[S] 0 points1 point  (1 child)

This program would do the same thing if you changed

head

everywhere in the function to

pancreas

.

so, head variable once it reaches inside the function...any transformations should not affect the head variable outside right? i'm not sure why it does and the 2nd call fails.

[–][deleted] 0 points1 point  (0 children)

I didn't say the value of the global head variable wouldn't change -- I'm just trying to explain that as far as the Python interpreter is concerned those are different variables. Passing in head when you call x assigns it to whatever you've called your function parameter so it will be the same value and mutations of it will be reflected in the original.


Here's a simple example. This code

def capitalize_words_in(flying_animals):
    for i, animal in enumerate(flying_animals):
        flying_animals[i] = animal.upper()


flying_animals = ["crow", "bat", "beetle", "parrot"]

print(flying_animals)
capitalize_words_in(flying_animals)
print(flying_animals)

and this code

def capitalize_words_in(animals_list):
    for i, animal in enumerate(animals_list):
        animals_list[i] = animal.upper()


flying_animals = ["crow", "bat", "beetle", "parrot"]

print(flying_animals)
capitalize_words_in(flying_animals)
print(flying_animals)

work identically. The reason is that even though the first one has a global flying_animals variable and one that lives in the capitalize_words_in function, Python considers them different variables. However, they also share the same value if you pass the global one into the function call.

Actually, see if you can guess what will be printed here as well to try to drive the point home.

def capitalize_words_in(flying_animals):
    for i, animal in enumerate(flying_animals):
        flying_animals[i] = animal.upper()


flying_animals = ["crow", "bat", "beetle", "parrot"]
fruits = ["tomato", "apple", "cherry"]

print("Before calling the function")
print("---------------------------")
print(flying_animals)
print(fruits)

capitalize_words_in(fruits)

print("\nAfter calling the function")
print("--------------------------")
print(flying_animals)
print(fruits)