python - Python 类在 Google Colaboratory 上表现异常
问题描述
我在 Google Colab 上创建了 Python 类 Student。然而,出于某种奇怪的原因,在初始化对象 Jane 之前调用了析构函数,这令人费解。
我在下面附上了 Google Colab 的输出。
当我在 1) JupyterLab 和 2) 我的本地机器上测试相同的代码时,我得到了我期望的输出(附在下面)。
有人能告诉我为什么类在 Google Colab 中表现得很奇怪吗?
class Student:
name = ''
section = ''
age = 0
def __init__(self, name, section, age):
self.name = name
self.section = section
self.age = age
def __del__(self):
print(f"{self.name} is graduating!")
def print_details(self):
print(f"Hi, my name is: {self.name} and I am in section {self.section}. My age is {self.age}")
ramu = Student('Ramu',3, 15)
ramu.print_details()
del(ramu)
jane = Student('Jane',1,14)
jane.print_details()
谷歌 Colab 输出
Hi, my name is: Ramu and I am in section 3. My age is 15
Ramu is graduating!
Jane is graduating!
Hi, my name is: Jane and I am in section 1. My age is 14
JupyterLab 和本地机器输出
Hi, my name is: Ramu and I am in section 3. My age is 15
Ramu is graduating!
Hi, my name is: Jane and I am in section 1. My age is 14
解决方案
推荐阅读
- wpf - WPF 继承的附加依赖属性是否“昂贵”?
- python - Python进程只在循环中产生一个进程
- javascript - 通过循环的动态数组元素
- javascript - 在 1 组或 2 组括号之间获取文本的表达式?[Javascript]
- android - GoogleService 初始化失败,状态:10,缺少来自名称为 google_app_id 的字符串资源的 google app id 值
- android - android从不是导航抽屉的活动中打开一个位于导航抽屉中的片段
- c# - C# HttpClient.SendAsync 等待抛出 NullReferenceException
- c# - 在单列选择中获取整行
- c++ - 复制 std:set 的好方法
到 std::set - android - Android 版本可以在 Genymotion 模拟器上更新吗?