不能做多线分配! [英] Can't do a multiline assignment!

查看:66
本文介绍了不能做多线分配!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间我发现在Python中发现你不能用
进行多行分配

并且行间有注释,我感到很震惊。


例如,假设我想将一堆变量分配给

初始单值。用C或类似语言你会做:


CONSTANT1 =

/ *这是一些常数* /

CONSTANT2 =

CONSTANT3 =


/ *这还有其他一些常数* /

常数=


1;


在Python中,你通常可以使用括号在

上分几行。但你不能用括号来分配

几行。为此,你可以使用续行字符

(''\''):


CONSTANT1 = \

CONSTANT2 = \

CONSTANT3 = \

1


但我意识到你不能这样做:


CONSTANT1 = \

#糟糕...语法错误

CONSTANT2 = \

CONSTANT3 = \#这也是一个语法错误

#这也是\

1


有谁知道一种方法将评论放在这样的行之间?

我觉得这个限制非常烦人。

I was shocked a while ago when a discovered that in Python you can''t
do a multiline assignment
with comments between the lines.

For example, let''s say I want to assign a bunch of variables to an
initial, single value. In C or a similar language you would do:

CONSTANT1 =
/* This is some constant */
CONSTANT2 =
CONSTANT3 =

/*This is yet some other constant */
CONSTANT =

1;

In Python, you usually can use parentheses to split something over
several lines. But you can''t use parentheses for an assignment of
several lines. For that, you can use the line continuation character
(''\''):

CONSTANT1 = \
CONSTANT2 = \
CONSTANT3 = \
1

But I realized that you can''t do this:

CONSTANT1 = \
# Oops... syntax error
CONSTANT2 = \
CONSTANT3 = \ # This is also a syntax error
# And this too \
1

Does anyone know of a way to put the comments between lines like that?
I find this limitation very annoying.

推荐答案

s0 **** @ gmail.com 写道:

我很震惊不久之前,当一个人发现在Python中你不能用
进行多行分配

并且行间有注释。


例如,让我们说我想将一堆变量分配给

初始单值。用C或类似语言你会做:


CONSTANT1 =

/ *这是一些常数* /

CONSTANT2 =

CONSTANT3 =


/ *这还有其他一些常数* /

常数=


1;
I was shocked a while ago when a discovered that in Python you can''t
do a multiline assignment
with comments between the lines.

For example, let''s say I want to assign a bunch of variables to an
initial, single value. In C or a similar language you would do:

CONSTANT1 =
/* This is some constant */
CONSTANT2 =
CONSTANT3 =

/*This is yet some other constant */
CONSTANT =

1;



哎呀!没门!!如果你*希望*让你的代码难以阅读,我是

确定你可以找到很多方法来做到这一点,即使是在Python中,但是不要

期望Python改变以帮助你实现这样一个可疑的目标。


说真的,检查一下你想要这种语法的动机。它是否b / b $ b使代码更具可读性? (绝对不是。)它是否能让它更加可靠? (当然不是 - 考虑一下你需要在未来的某个时间将

CONSTANT2更改为不同的值。)


Gary Herron

Yuck! No way!! If you *want* to make your code that hard to read, I''m
sure you can find lots of ways to do so, even in Python, but don''t
expect Python to change to help you toward such a dubious goal.

Seriously, examine your motivations for wanting such a syntax. Does it
make the code more readable? (Absolutely not.) Does it make it more
maintainable. (Certainly not -- consider it you needed to change
CONSTANT2 to a different value some time in the future.)

Gary Herron


在Python中,你通常可以使用括号在
几行上拆分。但你不能用括号来分配

几行。为此,你可以使用续行字符

(''\''):


CONSTANT1 = \

CONSTANT2 = \

CONSTANT3 = \

1


但我意识到你不能这样做:


CONSTANT1 = \

#糟糕...语法错误

CONSTANT2 = \

CONSTANT3 = \#这也是一个语法错误

#这也是\

1


有谁知道一种方法把评论放在这样的行之间?

我发现这个限制非常烦人。
In Python, you usually can use parentheses to split something over
several lines. But you can''t use parentheses for an assignment of
several lines. For that, you can use the line continuation character
(''\''):

CONSTANT1 = \
CONSTANT2 = \
CONSTANT3 = \
1

But I realized that you can''t do this:

CONSTANT1 = \
# Oops... syntax error
CONSTANT2 = \
CONSTANT3 = \ # This is also a syntax error
# And this too \
1

Does anyone know of a way to put the comments between lines like that?
I find this limitation very annoying.


Yuck!没门!!如果你*希望*让你的代码难以阅读,我是
Yuck! No way!! If you *want* to make your code that hard to read, I''m

确定你可以找到很多方法,即使在Python中,但是不要
期望Python改变以帮助你实现这样一个可疑的目标。
sure you can find lots of ways to do so, even in Python, but don''t
expect Python to change to help you toward such a dubious goal.



嗯,我的实际代码不是看起来像那样。相信我,我喜欢干净的

代码。

Well, my actual code doesn''t look like that. Trust me, I like clean
code.


说真的,检查一下你想要这样语法的动机。它是否b / b $ b使代码更具可读性? (绝对不是。)它是否能让它更加可靠? (当然不是 - 考虑你需要在未来某个时间将

CONSTANT2更改为不同的值。)
Seriously, examine your motivations for wanting such a syntax. Does it
make the code more readable? (Absolutely not.) Does it make it more
maintainable. (Certainly not -- consider it you needed to change
CONSTANT2 to a different value some time in the future.)



是的,它使它更具可读性。是的,它确实让它(很多)更加可靠。主要是因为我没有这四个变量,我有大约三十个b $ b。而且我认为我不需要其中的一两个,但是

可能一次全部。

Yes, it makes it more readable. And yes, it does make it (a lot) more
maintainable. Mainly because I don''t have those four variables, I have
about thirty. And I think I won''t need to one or two of them, but
maybe all of them at once.


s0 **** @ gmail.com 写道:

>哎呀!没门!!如果你*希望*让你的代码难以阅读,我确定你可以找到很多方法来做到这一点,即使在Python中也是如此,但是不要期望Python改变帮助你实现这样一个可疑的目标。
>Yuck! No way!! If you *want* to make your code that hard to read, I''m
sure you can find lots of ways to do so, even in Python, but don''t
expect Python to change to help you toward such a dubious goal.



好​​吧,我的实际代码看起来并不像那样。相信我,我喜欢干净的

代码。


Well, my actual code doesn''t look like that. Trust me, I like clean
code.


>说真的,检查你想要这种语法的动机。它是否使代码更具可读性? (绝对不是。)它是否使其更易于维护。 (当然不是 - 考虑一下你需要在将来的某个时间将
CONSTANT2更改为不同的值。)
>Seriously, examine your motivations for wanting such a syntax. Does it
make the code more readable? (Absolutely not.) Does it make it more
maintainable. (Certainly not -- consider it you needed to change
CONSTANT2 to a different value some time in the future.)



使用字典?

Use a dictionary?


是的,它使它更具可读性。是的,它确实让它(很多)更加可靠。主要是因为我没有这四个变量,我有大约三十个b $ b。而且我想我不需要其中的一两个,但是

可能会立刻全部。
Yes, it makes it more readable. And yes, it does make it (a lot) more
maintainable. Mainly because I don''t have those four variables, I have
about thirty. And I think I won''t need to one or two of them, but
maybe all of them at once.


这篇关于不能做多线分配!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆