简体   繁体   English

foo = list,foo = []和foo = list()之间的区别?

[英]Difference between foo=list, foo=[], and foo=list()?

I'm new to python (but not programming in general) and don't really understand what is going on with lists. 我是python的新手(但不是一般的编程人员),并且真的不了解列表的内容。 I have thousands of test sets in a text file and each test set is 256 (could be dfferent) x,y points. 我在一个文本文件中有成千上万个测试集,每个测试集是256(可以是不同的)x,y点。 What I want to do is store each point in a test set to a list and then store each list in another list, effectively creating a 2D list. 我要做的是将测试集中的每个点存储到一个列表中,然后将每个列表存储在另一个列表中,从而有效地创建2D列表。 Two of the three options I've tried are error free but I don't know what the different options mean or why I should pick one over another. 我尝试过的三个选项中的两个是无错误的,但是我不知道不同的选项是什么意思,也不知道为什么我应该选择一个。 This is not the same as "Least Astonishment" and the Mutable Default Argument because that doesn't explain which of these is a list and what the other definitions are. 这与“最小惊讶”和“可变默认参数”不同,因为这不能解释其中的哪个是列表以及其他的定义是什么。

foo= list
foo.append(1)#TypeError: descriptor 'append' requires a 'list' object but received a 'int'

bar= []
bar.append(1)#no error

baz= list()
baz.append(1)#no error

The first one sets foo to the object list . 第一个将foo设置为对象list This is not an empty list, as you've discovered. 正如您所发现的,这不是一个空列表。 The object list is a built-in type ... and you now have an in-scope alias to the type. 对象list是内置类型...现在您具有该类型的作用域内别名。 You cannot append to a type; 您不能追加类型; hence the error message. 因此错误消息。

The other two are classic ways to initialize a variable to an empty list. 另外两种是将变量初始化为空列表的经典方法。 The middle one uses list literal notation to construct a list, while the last one invokes the list type object as a function to generate an instance of the type. 中间一个使用列表文字表示法构造一个列表,而最后一个使用list类型对象作为函数来生成该类型的实例。

When you're doing foo = list , you're actually assigning foo to a built-in python object of list. 当您执行foo = list ,实际上是将foo分配给foo = list的内置python对象。 bar = list() and baz = [] both initiates two new variables as new vacant list. bar = list()baz = []都将启动两个新变量作为新的空缺列表。 However in python var_ = [] assigning is a bit faster compared to var_ = list() . 但是在python中, var_ = []分配要比var_ = list()快一些。

This is because foo = list makes foo an alias for the list type. 这是因为foo = list使foo成为list类型的别名。 No actual list object has been instantiated in that line. 该行中没有实例化任何实际的list对象。 However, if you have a list object, you can use list.append() (or foo.append() ) in order to append to it. 但是,如果您有一个list对象,则可以使用list.append() (或foo.append() )来附加到该对象。

Like so: 像这样:

foo = list
bar = [1]

list.append(bar, 2)
# bar is now [1, 2]

foo.append(bar, 3)
# bar is now [1, 2, 3]

Since your example code calls foo.append() with only one argument (an int), the exception complains that it needs a list and not an int . 由于您的示例代码仅使用一个参数(一个int)调用foo.append() ,因此该异常会抱怨它需要一个list而不是一个int

Note: This is not a special feature of the list class. 注意:这不是列表类的特殊功能。 All instance methods can be called in both forms: some_object.func(a, b, c) or SomeType.func(some_object, a, b, c) 可以两种形式调用所有实例方法: some_object.func(a, b, c)SomeType.func(some_object, a, b, c)

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

相关问题 foo.bar()和bar(foo)之间的区别? - Difference between foo.bar() and bar(foo)? “foo is None”和“foo == None”之间有什么区别吗? - Is there any difference between “foo is None” and “foo == None”? 类foo,类foo()和类foo(对象)之间的区别? - Difference between class foo , class foo() and class foo(object)? 如果元素在列表中,则打印“ Foo” - Print “Foo” if an element is in a list 'list((x,))。extend([x])'和'foo = list((x,))之间有什么区别? foo.extend([x])' - What's the different between 'list((x,)).extend([x])' and 'foo=list((x,)); foo.extend([x])' Python中类foo和类foo(对象)之间的区别 - Difference between class foo and class foo(object) in Python 为什么调用 super().foo 和 super().__getattribute__("foo") 有区别 - why is there difference between calling super().foo and super().__getattribute__("foo") 提升异常,“foo”和提高异常(“foo”)之间的区别? - Difference between raise Exception, “foo” and raise Exception(“foo”)? super(Foo,self)和super(Foo,self .__ class__)之间的区别? - Difference between super(Foo, self) and super(Foo, self.__class__)? xpath //*[.="Foo"] 和 xpath //*["Foo"] 在谓词中只有一个字符串有什么区别? - What is the difference between the xpath //*[.="Foo"] and the xpath //*["Foo"] with only a string in the predicate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM