为什么这样? [英] Why do this?

查看:63
本文介绍了为什么这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,不是真正专注于python,但感觉就像这里的人们可以为我解释一下吧。


现在,我开始编程的时候我是BBC Basic的8岁。


我从未上过任何正式课程,而且我从未成为一名

专家程序员。我是一个普通/业余爱好者的程序员,有很多

语言,但我不能用任何

来做任何真正花哨的技巧。 (虽然Python可能会把我推向更高级的东西,

现在我开始得到所有__method__ thingies和运算符

都是关于的)


多年来我学会了做以下事情,我喜欢

因为可读性这样做,所以Python似乎是这样的。 $ b专注于: -


打印有+数字+皮肤的方式+ furryanimal


但是现在,我看到这样的东西到处都是;


print(" There %s s%皮肤%s&%;%(数字,毛茸茸))


现在我明白当

处理数字时可以有额外的格式化好处,小数点等等。但对我来说,对于字符串来说,

第二种情况比第一种情况更难阅读。


我希望我''我没有密集。


结果是我已经完全避免了打印%s %(东西)

因为它似乎只是让事情变得复杂。

Ta,:)


马特。


此电子邮件是保密的,可能具有特权。如果您不是目标收件人,请立即通知发件人并从您的计算机中删除该电子邮件。


您不应复制电子邮件,将其用于任何目的或将其内容透露给任何其他人。

请注意,任何观点或意见均以此电子邮件可能是作者的个人信息,并不一定代表Digica的观点或意见。

收件人有责任检查此电子邮件是否存在病毒。 Digica对此电子邮件传播的任何病毒造成的任何损害不承担任何责任。


英国:Phoenix House,Colliers Way,Nottingham,NG8 6AT UK

接待处电话:+ 44(0)115 977 1177

支持中心:0845 607 7070

传真:+ 44(0)115 977 7000
http://www.digica.com


南非: 3楼,Parc du Cap,Mispel Road,Bellville,7535,南非

电话:+ 27(0)21 957 4900

传真:+ 27(0)21 948 3135
http://www.digica.com

Ok, not really python focused, but it feels like the people here could
explain it for me :)

Now, I started programming when I was 8 with BBC Basic.

I never took any formal classes however, and I have never become an
expert programmer. I''m an average/hobbyist programmer with quite a few
languages under my belt but I cant do any really fancy tricks with any
of them. (although Python might be nudging me into more advanced things,
now I''m starting to get what all the __method__ thingies and operators
are all about)

I learned over the years to do things like the following, and I like
doing it like this because of readability, something Python seems to
focus on :-

Print "There are "+number+" ways to skin a "+furryanimal

But nowadays, I see things like this all over the place;

print("There are %s ways to skin a %s" % (number, furryanimal))

Now I understand there can be additional formatting benefits when
dealing with numbers, decimal places etc.. But to me, for strings, the
second case is much harder to read than the first.

I hope I''m not being dense.

The result is that I have pathalogically avoided print "%s" % (thing)
because it seems to just over complicate things.
Ta, :)

Matt.

This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email fromyour computer.

You should not copy the email, use it for any purpose or disclose its contents to any other person.
Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions ofDigica.
It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com

推荐答案

在< ma ****************************** *********@蟒蛇。 org>,Matthew Warren

写道:
In <ma***************************************@python. org>, Matthew Warren
wrote:

多年来我学会了做以下事情,我喜欢

因为可读性而这样做,Python看起来好像是
专注于: -


打印有+数字+ "皮肤的方式+ furryanimal


但是现在,我看到这样的东西到处都是;


print(" There %s s%皮肤%s&%;%(数字,毛茸茸))


现在我明白当

处理数字时可以有额外的格式化好处,对于我来说,对于字符串来说,

第二种情况比第一种情况更难阅读。
I learned over the years to do things like the following, and I like
doing it like this because of readability, something Python seems to
focus on :-

Print "There are "+number+" ways to skin a "+furryanimal

But nowadays, I see things like this all over the place;

print("There are %s ways to skin a %s" % (number, furryanimal))

Now I understand there can be additional formatting benefits when
dealing with numbers, decimal places etc.. But to me, for strings, the
second case is much harder to read than the first.



对我而言,反过来说 - 我发现第二个更具可读性

特别是没有语法高亮显示哪个字符在里面

并且在引号之外。


第二个字符串清晰可见分离

文字和变量,即使没有颜色。


第二种方式的另一个优点是更容易本地化和其他

任务,其中句子本身不是硬编码,但从文件读取,

数据库等


''%s''自动将对象转换为字符串。相当于没有字符串格式的第二个例子的

将是::


print''有''+ str(number)+''方法皮肤''+ str(毛茸茸的)


Ciao,

Marc''BlackJack''Rintsch

For me it''s the other way around -- I find the second one more readable
especially without syntax highlighting to see which characters are inside
and which are outside the quotes.

With the second one there''s a clear visual separation of the string
literal and the variables, even without colors.

Another advantage of the second way is much easier localization and other
tasks where the sentence itself is not hardcoded but read from files,
databases, etc.

And the ''%s'' automatically converts the objects to strings. The
equivalent to the second example without string formatting would be::

print ''There are'' + str(number) + '' ways to skin a '' + str(furryanimal)

Ciao,
Marc ''BlackJack'' Rintsch


" Matthew Warren" < Ma ************ @ Digica.comwrote:
"Matthew Warren" <Ma************@Digica.comwrote:

打印"有+ number +皮肤的方式+ furryanimal


但是现在,我看到这样的东西到处都是;


print(" There %s s%皮肤%s&%;%(数字,毛茸茸))


现在我明白当

处理数字时可以有额外的格式化好处,小数位等。但对我而言,对于字符串,

第二种情况比第一种情况更难阅读。
Print "There are "+number+" ways to skin a "+furryanimal

But nowadays, I see things like this all over the place;

print("There are %s ways to skin a %s" % (number, furryanimal))

Now I understand there can be additional formatting benefits when
dealing with numbers, decimal places etc.. But to me, for strings, the
second case is much harder to read than the first.



分开从你在第二个中加入的虚假括号中,你也错过了这个变种:


print"有,数字,方式皮肤一个,furryanimal


这只适用于打印,但不适用于字符串的其他用途,但它与我使用的主要原因相关的是
格式化字符串而不是连接。


我的第一个选择的问题是我已经写了很多次b / b



print"有&q uot; + number +" skin to a+ furryanimal


或至少相当于它的东西。如果我试图用格式字符串犯同样的错误

它会向我跳出错误:


"有%s s to to a a% S" %(数字,furryanimal)


此外,有一个名为''number'的str类型的变量似乎有悖常理(并且

可能容易出错),所以我怀疑我可能需要这样的东西:


print"有+ str(number)+塑造+ furryanimal


的方法,但格式字符串可以免费转换。


优先选择格式字符串的另一个主要原因就是他们让它更容易重构代码。如果您想要将格式化的格式从

移开,那么提取单个

字符串要比清理它更容易串联。

apart from the spurious parentheses you added in the second one, you also
missed out this variant:

print "There are", number, "ways to skin a", furryanimal

That only works for print though, not for other uses of strings, but it is
related to the main reason I use format strings instead of concatenation.

The problem I have with your first option is the large number of times I''ve
written:

print "There are"+number+"ways to skin a"+furryanimal

or at least something equivalent to it. If I try to make the same mistake
with a format string it jumps out to me as wrong:

"There are%sways to skin a%s" % (number, furryanimal)

Also, having a variable of type str called ''number'' seems perverse (and
probably error prone), so I suspect I might need something like:

print "There are "+str(number)+" ways to skin a "+furryanimal

but the format string does the conversion for free.

The other main reason for preferring format strings is that they make it
easier to refactor the code. If you ever want to move the message away from
where the formatting is done then it''s a lot easier to extract a single
string than it is to clean up the concatenation.


Duncan Booth写道:
Duncan Booth wrote:

print"有+ number +方式皮肤a+ furryanimal


或至少相当于它的东西。如果我试图用格式字符串犯同样的错误

它会向我跳出错误:


"有%s s to to a a% S" %(number,furryanimal)
print "There are"+number+"ways to skin a"+furryanimal

or at least something equivalent to it. If I try to make the same mistake
with a format string it jumps out to me as wrong:

"There are%sways to skin a%s" % (number, furryanimal)



与此相关,使用序列进行格式化也更具可读性

当存在复杂的interpunction和引用字符时现在,

是这样的:


print"''" + var1 +"'',''" + var2''" +"," + var3


以上内容更具可读性


print"''%s'',' '%s'',%s" %(var1,var2,var3)

Related to this, formatting with sequences is also much more readable
when there are complex interpunction and quoting characters present,
like this:

print "''"+var1+"'',''"+var2''"+","+var3

the above is much more readable as

print "''%s'', ''%s'', %s" % (var1, var2, var3)


这篇关于为什么这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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