1

我已经看到在 Python 中编写相同的两种方式:

while 1: 
   # do something here

while True:
   # do something here

哪个更pythonic?

我已经知道这True 已经是一个关键字差异等,但我在问哪个更pythonic

4

1 回答 1

3

没有实际区别。 1并且True在 python 中是相等的,这在未来也得到了保证,所以可以使用你最喜欢的外观。

至于哪个是pythonic,我认为完全避免这种循环是pythonic,除非在极少数情况下你确实打算有一个无限循环。

您几乎总是可以使用显式终止条件而不是 break 语句来编写它们,并且显式优于隐式


两者在标准库中的使用频率大致相同。96 次出现while True

/usr/lib/python2.7$ grep -rn "while True:"
_abcoll.py:292:            while True:
_abcoll.py:478:            while True:
_abcoll.py:531:            while True:
argparse.py:1807:            while True:
base64.py:287:    while True:
base64.py:302:    while True:
calendar.py:162:        while True:
chunk.py:28:while True:
chunk.py:34:    while True:
codecs.py:458:        while True:
codecs.py:529:        while True:
ConfigParser.py:478:        while True:
decimal.py:2141:            while True:
decimal.py:2331:            while True:
decimal.py:2681:        while True:
decimal.py:2990:            while True:
decimal.py:3124:        while True:
decimal.py:3205:            while True:
decimal.py:5695:            while True:
difflib.py:1466:        while True:
difflib.py:1556:        while True:
difflib.py:1573:        while True:
difflib.py:1580:        while True:
filecmp.py:67:        while True:
fractions.py:245:        while True:
gzip.py:202:            while True:
gzip.py:208:            while True:
gzip.py:248:                while True:
gzip.py:502:        while True:
httplib.py:273:        while True:
httplib.py:406:        while True:
httplib.py:411:            while True:
httplib.py:572:        while True:
httplib.py:610:        while True:
httplib.py:747:        while True:
locale.py:134:            while True:
mailbox.py:217:            while True:
mailbox.py:534:        while True:
mailbox.py:652:                while True:
mailbox.py:814:        while True:
mailbox.py:850:        while True:
mailbox.py:856:                while True:
mailbox.py:1221:        while True:
mailbox.py:1227:        while True:
mailbox.py:1246:        while True:
mailbox.py:1251:        while True:
mailbox.py:1279:        while True:
mailbox.py:1342:            while True:
mailbox.py:1352:                while True:
mailbox.py:1359:                while True:
mailbox.py:1364:            while True:
mailbox.py:1385:            while True:
mailbox.py:1395:            while True:
mimetypes.py:243:            while True:
pdb.py:1312:    while True:
pickletools.py:1841:    while True:
posixpath.py:299:    while True:
pty.py:146:    while True:
pyclbr.py:196:                    while True:
pyclbr.py:284:    while True:
pyclbr.py:308:    while True:
py_compile.py:146:        while True:
pydoc.py:537:        while True:
pydoc.py:1762:        while True:
_pyio.py:559:        while True:
_pyio.py:953:            while True:
_pyio.py:1013:            while True:
_pyio.py:1896:        while True:
sets.py:356:            while True:
shlex.py:123:        while True:
socket.py:349:            while True:
socket.py:372:            while True:
socket.py:427:                while True:
socket.py:445:            while True:
socket.py:474:            while True:
socket.py:514:        while True:
SocketServer.py:153:    while True:
ssl.py:196:            while True:
subprocess.py:476:    while True:
sysconfig.py:406:    while True:
tarfile.py:529:            while True:
tarfile.py:534:            while True:
tarfile.py:566:            while True:
tarfile.py:836:            while True:
tarfile.py:859:        while True:
tarfile.py:902:        while True:
tarfile.py:1168:            while True:
tarfile.py:1384:        while True:
tarfile.py:1579:                while True:
tarfile.py:2317:        while True:
tarfile.py:2379:        while True:
tarfile.py:2508:        while True:
threading.py:255:                while True:
uu.py:105:        while True:
_weakrefset.py:97:        while True:
zipfile.py:579:        while True:

和 116 次出现while 1。请注意,较旧的代码更有可能具有这种形式,因为bool在早期的 python 中并不总是存在。

/usr/lib/python2.7$ grep -rn "while 1:" *.py
aifc.py:309:        while 1:
aifc.py:962:        while 1:
audiodev.py:252:    while 1:
binhex.py:257:    while 1:
binhex.py:265:    while 1:
binhex.py:295:            while 1:
binhex.py:377:        while 1:
binhex.py:481:    while 1:
binhex.py:492:        while 1:
cgi.py:251:        while 1:
cgi.py:683:        while 1:
cgi.py:696:        while 1:
cgi.py:729:        while 1:
code.py:227:        while 1:
compileall.py:141:            while 1:
DocXMLRPCServer.py:41:        while 1:
fpformat.py:141:        while 1:
ftplib.py:199:            while 1:
ftplib.py:407:        while 1:
ftplib.py:431:        while 1:
ftplib.py:462:        while 1:
ftplib.py:484:        while 1:
ftplib.py:691:                while 1:
ftplib.py:709:                while 1:
ftplib.py:731:                while 1:
ftplib.py:747:                while 1:
ftplib.py:904:        while 1:
getpass.py:97:    while 1:
heapq.py:346:    while 1:
heapq.py:348:            while 1:
imaplib.py:864:        while 1:
imaplib.py:987:        while 1:
_LWPCookieJar.py:111:            while 1:
mailbox.py:2021:        while 1:
mailbox.py:2039:        while 1:
mailbox.py:2050:        while 1:
mailbox.py:2109:        while 1:
mailbox.py:2117:        while 1:
mailbox.py:2164:        while 1:
mailbox.py:2172:        while 1:
mailcap.py:63:    while 1:
markupbase.py:269:        while 1:
markupbase.py:323:        while 1:
markupbase.py:345:            while 1:
markupbase.py:358:        while 1:
mhlib.py:305:        while 1:
mhlib.py:616:            while 1:
mhlib.py:915:    while 1:
mhlib.py:920:            while 1:
mimetools.py:240:    while 1:
mimetools.py:247:    while 1:
mimetypes.py:213:        while 1:
mimify.py:86:        while 1:
mimify.py:98:    while 1:
mimify.py:111:    while 1:
mimify.py:135:    while 1:
mimify.py:164:    while 1:
mimify.py:241:    while 1:
mimify.py:266:    while 1:
mimify.py:292:    while 1:
mimify.py:315:    while 1:
mimify.py:390:            while 1:
mimify.py:407:        while 1:
_MozillaCookieJar.py:58:            while 1:
multifile.py:15:while 1:
multifile.py:114:        while 1:
netrc.py:38:        while 1:
netrc.py:58:                while 1:
netrc.py:73:            while 1:
nntplib.py:239:            while 1:
nntplib.py:559:        while 1:
nntplib.py:583:        while 1:
os.py:545:            while 1:
pdb.py:40:    while 1:
pickle.py:856:            while 1:
platform.py:166:    while 1:
platform.py:966:    while 1:
quopri.py:70:    while 1:
quopri.py:128:    while 1:
random.py:394:        while 1:
random.py:464:        while 1:
random.py:517:            while 1:
random.py:540:            while 1:
re.py:310:        while 1:
rfc822.py:148:        while 1:
shlex.py:287:    while 1:
shutil.py:48:    while 1:
site.py:424:        while 1:
smtplib.py:352:        while 1:
smtplib.py:847:    while 1:
sre_parse.py:307:    while 1:
sre_parse.py:325:    while 1:
sre_parse.py:393:    while 1:
sre_parse.py:406:                while 1:
sre_parse.py:425:            while 1:
sre_parse.py:536:                        while 1:
sre_parse.py:549:                        while 1:
sre_parse.py:573:                    while 1:
sre_parse.py:600:                    while 1:
sre_parse.py:640:                while 1:
sre_parse.py:708:    while 1:
sre_parse.py:718:                    while 1:
telnetlib.py:587:        while 1:
telnetlib.py:608:        while 1:
telnetlib.py:616:        while 1:
telnetlib.py:732:        while 1:
tokenize.py:286:    while 1:                                   # loop over lines in stream
urllib.py:266:                while 1:
urlparse.py:282:    while 1:
wave.py:135:        while 1:
weakref.py:153:        while 1:
weakref.py:359:        while 1:
xdrlib.py:214:        while 1:
xmllib.py:187:        while 1:
xmlrpclib.py:1461:        while 1:
zipfile.py:1095:            while 1:
于 2013-03-14T06:51:51.110 回答