Python入门 lab4

1 Stack frame tracing

1.1 hours_to_seconds(20)

3 stack frames are created when executing hours_to_seconds(20):
hours_to_seconds
hours_to_minutes
minutes_to_seconds

1.2 days_to_seconds(6)

5 stack frames are created when executing days_to_seconds(6):
days_to_seconds
days_to_hours
hours_to_seconds
hours_to_minutes
minutes_to_seconds

1.3 total

6 stack frames (including global frame) are created in total:
days_to_seconds
days_to_hours
hours_to_seconds
hours_to_minutes
minutes_to_seconds


2 Stack Frame Visualization

(every result matches the expectation)

2.1 hours_to_seconds(20)

running result:

在这里插入图片描述
在这里插入图片描述

2.2 days_to_seconds(6)

running result:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


3 Updating list in place

3.1 trace the code manually

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:

在这里插入图片描述

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 objects rather than the objects themselves. When we assign list1 to list3, we are actually making list3 reference the same list object as list1. So when we modify list1, list3 also reflects those changes because they both refer to the same list object.

3.3

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

3.4

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 object reference. This is because the function parameter passes 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

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

4.2

There are 5 local variables in the program.
Inside the function multiply_local, there are three local variables named:
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

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

4.4

Inside either of the 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: (every result matches the expectation)

在这里插入图片描述

  • 19
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值