简体   繁体   English

在 Python 中实例化 Class 的区别

[英]Difference in instantiating a Class in Python

I am currently working on a introductory project for kivy.我目前正在为 kivy 做一个介绍性项目。 I got an error while trying to understand the concept by playing around with the codes.我在尝试通过玩代码来理解这个概念时遇到了一个错误。 I would like to know what is the difference between the 2 codes below.我想知道下面两个代码有什么区别。 Thank you.谢谢你。

Orginal:原件:

sm = WindowManager(), sm = 窗口管理器(),

sm.current = "second" sm.current = "第二"

Current:当前的:

WindowsManager().current = "second" WindowsManager().current = "秒"

method 1: you create a WindowManager instance and call it sm, then you assign the attribute current with "second".方法1:创建一个WindowManager 实例并将其命名为sm,然后将属性current 分配为“second”。

method 2: you create a temp WindowManager instance and assign the attribute current with "second".方法 2:您创建一个临时 WindowManager 实例并将当前属性分配为“秒”。 Then the temp instance will be removed by garbage collection.然后临时实例将被垃圾收集删除。 (ie nothing really happens.) (即没有真正发生。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM