[英]How to instantiate Python list without syntactic sugar
I want to explain object instantiation in Python to some students. 我想向一些学生解释Python中的对象实例化。 They have been coding for a couple of sessions. 他们已经编写了几个会话。 Now, I'd like to reveal them that they have been working with objects all along. 现在,我想向他们透露他们一直在处理物体。 For example, the floats, ints, booleans, lists they have been using are all objects. 例如,它们一直使用的浮点数,整数,布尔值,列表都是对象。
Now I want them to show how to instantiate objects. 现在我希望它们展示如何实例化对象。 I'd like to show them by using the objects they are already familiar with (eg, lists). 我想通过使用他们已经熟悉的对象(例如,列表)来展示它们。 But, is there any way to instantiate an empty list very explicitly, eg myList = List()
? 但是,有没有办法非常明确地实例化一个空列表,例如myList = List()
?
I'm only familiar with syntactic sugar approaches, such as 我只熟悉句法糖方法,比如
myList = []
or using the built-in function 或使用内置功能
myList = list()
Can it be done using the regular instantiation syntax? 可以使用常规实例化语法来完成吗?
Despite the fact that list
is writen without the cappital "L", it IS a type in Python. 尽管list
是在没有cappital“L”的情况下编写的,但它是Python中的一种类型。 So, when you do the l = list()
, you're not calling any built-in function or syntatic suggar, you're instantiating the type list. 因此,当你执行l = list()
,你没有调用任何内置函数或者合成的suggar,你就是在实例化类型列表。
The uncapitalized "L" of the list
, is the same as the "D" in dict
. list
大写的“L”与dict
的“D”相同。 It's because both are Built-In Types . 这是因为两者都是内置类型 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.