Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Python Functions: Stack Frames, List Mutability, and Variable Scope

Tech 2

1 Stack Frame Tracing

1.1 hours_to_seconds(20)

When executing hours_to_seconds(20), 3 stack frames are created:

  • hours_to_seconds
  • hours_to_minutes
  • minutes_to_seconds

1.2 days_to_seconds(6)

When executing days_to_seconds(6), 5 stack frame are created:

  • days_to_seconds
  • days_to_hours
  • hours_to_seconds
  • hours_to_minutes
  • minutes_to_seconds

1.3 Total Stack Frames

In total, 6 stack frames (including the global frame) are created:

  • days_to_seconds
  • days_to_hours
  • hours_to_seconds
  • hours_to_minutes
  • minutes_to_seconds

2 Stack Frame Visualization

(All results match expectations.)

2.1 hours_to_seconds(20)

Running result:

Stack frame for hours_to_seconds(20) - step 1

Stack frame for hours_to_seconds(20) - step 2

2.2 days_to_seconds(6)

Running result:

Stack frame for days_to_seconds(6) - step 1

Stack frame for days_to_seconds(6) - step 2

Stack frame for days_to_seconds(6) - step 3

3 Updating a List In-Place

3.1 Manual Code Trace

before update
list1: [0, 1, 2, 3]
list2: [0, 1, 2, 3]
list3: [0, 1, 2, 3]
after update
list1: [0, 1, 4, 9]
list2: [0, 1, 2, 3]
list3: [0, 1, 4, 9]

3.2 Running Result

List update result

list2 did not change, while list3 did change after calling the update_list function on list1.

The reason is that in Python, lists are mutable objects, and variables store references to objectss rather than the objects themselves. When we assign list1 to list3, we are actually making list3 reference the same list object as list1. Therefore, when we modify list1, list3 also reflects those changes because they both refer to the same list object.

3.3 Explanation

This example demonstrates the concept of references. When we assign a mutable object to multiple variables, those variables actually reference the same object. Consequently, modifications made to one variable will affect other variables that reference the same object.

3.4 Implications for Function Calls

Regarding object references in function calls, when we pass a mutable object as a parameter to a function, the function can modify the object because it operates on the same reference. This is because the parameter receives the reference to the object, not a copy of the object itself. Hence, for mutable objects, modifications made within the function persist after the function call. However, for immutable objects, modifications made within the function do not affect the variables outside the function, because immutable objects create a new object when modified.

4 Global vs Local Variables

4.1 Global Variable

There is 1 global variable in the program named num with a value of 10.

4.2 Local Variables

There are 5 local variables in the program. Inside the function multiply_local, there are three local variables:

  • num with a value of 40
  • number with a value of 10
  • multiple with a value of 4

Inside the function multiply_global:

  • number with a value of 10
  • multiple with a value of 4

4.3 Parameter Names

The names used as parameter names in the program are number and multiple.

4.4 Variable Behavior

Inside either function call, the value of the variable num will always be 40.

After the multiply_local function call, the value of the global variable num remains unchanged at 10.

After the multiply_global function call, the value of the global variable num will be modified to 40.

Therefore, the expected output is:

num inside function: 40
num after multiple_local: 10
num inside function: 40
num after multiple_glocal: 40

Running result: (Matches expectations.)

Global vs local variable output


Note: This article is based on a Python introductory lab exercise focusing on stack frames, mutable objects, and variable scope.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.