简体   繁体   English

如何在没有语法糖的情况下实例化Python列表

[英]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.

相关问题 有无语法糖的Python装饰器之间的区别? - Difference between Python decorator with and without syntactic sugar? 列表推导式是 Python 3 中`list(generator expression)` 的语法糖吗? - Are list comprehensions syntactic sugar for `list(generator expression)` in Python 3? 在 python 中,属性装饰器如何在内部使用语法糖(@)工作? - How does property decorator work internally using syntactic sugar(@) in python? Python语法糖:函数arg别名 - Python syntactic sugar: function arg aliases python:语法糖-是官方的吗? 文档在哪里描述? - python: syntactic sugar - is it official? where is it described in documentation? 对Python装饰器和“语法糖”感到困惑 - Confused about Python decorators and “syntactic sugar” 通过预分配和`my_list [x] = y`填充python列表作为`__setitem__`的语法糖。 - Filling a python list by preallocating and `my_list[x] = y` as syntactic sugar for `__setitem__`. 接口只是“语法糖”吗? - Are Interfaces just “Syntactic Sugar”? Python函数或语法糖返回生成器表达式的所有元素? - Python function or syntactic sugar to return all elements of a generator expression? 如何为切片numpy数组提供语法糖? - How can I provide syntactic sugar for slicing a numpy array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM