JavaScript中的尾随逗号是标准的,还是像Chrome和Firefox这样的大多数浏览器只是容忍它们?
我认为它们是标准的,但IE8遇到一个当然IE浏览器不支持的东西几乎不意味着它不标准.
这是我的意思的一个例子(在书籍数组的最后一个元素之后):
var viewModel = {
books: ko.observableArray([
{ title: "..", display: function() { return ".."; } },
{ title: "..", display: function() { return ".."; } },
{ title: "..", display: function() { return ".."; } }, // <--right there
]),
currentTemplate: ko.observable("bookTemplate1"),
displayTemplate: function() { return viewModel.currentTemplate(); }
};
Run Code Online (Sandbox Code Playgroud) 一些代码样式工具推荐这个,我记得看到一些unix命令行工具警告缺少空行.
有多余空行的原因是什么?
在单个元素元组的情况下,需要尾随逗号.
a = ('foo',)
Run Code Online (Sandbox Code Playgroud)
那个有多个元素的元组怎么样?似乎尾随逗号是否存在,它们都是有效的.它是否正确?在我看来,使用尾随逗号更容易编辑.这是一种糟糕的编码风格吗?
a = ('foo1', 'foo2')
b = ('foo1', 'foo2',)
Run Code Online (Sandbox Code Playgroud) 我正在玩python,我意识到除非直接使用,否则我们不需要使用'+'运算符来连接字符串.
例如:
string1 = 'Hello' 'World' #1 works fine
string2 = 'Hello' + 'World' #2 also works fine
string3 = 'Hello'
string4 = 'World'
string5 = string3 string4 #3 causes syntax error
string6 = string3 + string4 #4 works fine
Run Code Online (Sandbox Code Playgroud)
现在我有两个问题:
python string optimization concatenation string-concatenation
我正在编写一个代码,突然看到","不会导致任何编译错误.为什么?
我的意思是说
public enum A {
B, C, ; // no compilation error
}
Run Code Online (Sandbox Code Playgroud)
但
int a, b, ; // compilation error
Run Code Online (Sandbox Code Playgroud) 注意:不要相信原始问题中的任何内容是正确的,请转到底部进行更新。
原始问题
我相信 PEP8 风格指南说
some_kind_of_list = [
1, 2, 3,
4, 5, 6
]
def function_that_takes_long_arguments(
long_argument_1,
long_argument_2
):
return long_argument_1
Run Code Online (Sandbox Code Playgroud)
和
some_kind_of_list = [
1, 2, 3,
4, 5, 6
]
def function_that_takes_long_arguments(
long_argument_1,
long_argument_2
):
return long_argument_1
Run Code Online (Sandbox Code Playgroud)
是可以接受的,但是使用其中一个是否有意义,例如,如果我在以后的生活中转向 C++?
更新
为了直接记录,函数定义的常见样式是:
some_kind_of_list = [
1, 2, 3,
4, 5, 6
]
def function_that_takes_long_arguments(
long_argument_1,
long_argument_2
):
return long_argument_1
Run Code Online (Sandbox Code Playgroud)
而对于函数调用,它是:
some_kind_of_list = [
1, 2, 3,
4, 5, 6
]
def function_that_takes_long_arguments(
long_argument_1,
long_argument_2
):
return long_argument_1
Run Code Online (Sandbox Code Playgroud) 在我正在查看的代码中,我看到了一些像这样的类方法:
class A(B):
def method1(self,):
do_something
def method2(self,):
do_something_else
Run Code Online (Sandbox Code Playgroud)
为什么作家在自己后面留下逗号,他/她的目的是什么?
我有以下内容namedtuple:
from collections import namedtuple
Product = namedtuple(
'Product',
'product_symbol entity unique_id as_of_date company_name followers linkedin_employees linkedin industry date_added date_updated description website sector product_industry'
)
Run Code Online (Sandbox Code Playgroud)
声明它的最佳方法是什么,所以它不超过80行的Python行限制?
当我注意到代码打[0,]*6,不返回[0,0,0,0,0,0,],而是[0,0,0,0,0,0].你能解释一下原因吗?
我正在尝试将一些Python代码移植到Java.我不熟悉Python,以前从未在任何语言中看过这个:
return [c,] + s
Run Code Online (Sandbox Code Playgroud)
这条线究竟意味着什么?特别是[c,]部分.是组合两个数组还是什么?s是整数数组,c是整数.完整功能如下(来自维基百科:http://en.wikipedia.org/wiki/Ring_signature)
def sign(self,m,z):
self.permut(m)
s,u = [None]*self.n,random.randint(0,self.q)
c = v = self.E(u)
for i in range(z+1,self.n)+range(z):
s[i] = random.randint(0,self.q)
v = self.E(v^self.g(s[i],self.k[i].e,self.k[i].n))
if (i+1)%self.n == 0: c = v
s[z] = self.g(v^u,self.k[z].d,self.k[z].n)
return [c,] + s
Run Code Online (Sandbox Code Playgroud)
非常感谢!
曾几何时,我在有关编译器的计算机科学课程中编写了一个 C 编译器。部分工作涉及使用Backus Naur Form ( BNF ) 中的 C 语法,如下所示。让我感到奇怪的是,初始化列表的语法允许列表以逗号结尾(所谓的悬空逗号)。我在我的编译器和其他编译器上尝试了它,并确认它是允许的。
例子:
<initializer> ::= <assignment-expression>
| { <initializer-list> }
| { <initializer-list> , }
Run Code Online (Sandbox Code Playgroud)
在C中:
int array[] = { 1, 2, 3, };
Run Code Online (Sandbox Code Playgroud)
我的问题:直到今天,悬空逗号仍然是 C 的一部分,其他语言似乎也是如此。为什么他们没有被移除?(假设它们没有任何“适当的”功能(我可能是错的),为什么它们传播到其他语言?)
python ×7
coding-style ×2
arrays ×1
c ×1
enumeration ×1
eof ×1
java ×1
javascript ×1
list ×1
optimization ×1
parsing ×1
pep8 ×1
python-2.7 ×1
string ×1
syntax ×1
tuples ×1