国产一区二区精品-国产一区二区精品久-国产一区二区精品久久-国产一区二区精品久久91-免费毛片播放-免费毛片基地

千鋒教育-做有情懷、有良心、有品質的職業教育機構

手機站
千鋒教育

千鋒學習站 | 隨時隨地免費學

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

關注千鋒學習站小程序
隨時隨地免費學習課程

當前位置:首頁  >  技術干貨  > python閱讀代碼有助于養成優秀的代碼風格

python閱讀代碼有助于養成優秀的代碼風格

來源:千鋒教育
發布人:xqq
時間: 2023-11-07 19:12:23 1699355543

優秀的代碼不需要文檔”,這句話雖然說的有些夸張的成份,但是也并無一定道理。優秀的項目中的代碼,注釋占的比重是相當大的。比方tornado框架中的代碼:

classHTTPServer(TCPServer,Configurable,

httputil.HTTPServerConnectionDelegate):

r"""Anon-blocking,single-threadedHTTPserver.

Aserverisdefinedbyasubclassof.HTTPServerConnectionDelegate,

or,forbackwardscompatibility,acallbackthattakesan

.HTTPServerRequestasanargument.Thedelegateisusuallya

tornado.web.Application.

HTTPServersupportskeep-aliveconnectionsbydefault

(automaticallyforHTTP/1.1,orforHTTP/1.0whentheclient

requestsConnection:keep-alive).

IfxheadersisTrue,wesupportthe

X-Real-Ip/X-Forwarded-Forand

X-Scheme/X-Forwarded-Protoheaders,whichoverridethe

remoteIPandURIscheme/protocolforallrequests.Theseheaders

areusefulwhenrunningTornadobehindareverseproxyorload

balancer.Theprotocolargumentcanalsobesettohttps

ifTornadoisrunbehindanSSL-decodingproxythatdoesnotsetoneof

thesupportedxheaders.

TomakethisserverserveSSLtraffic,sendthessl_optionskeyword

argumentwithanssl.SSLContextobject.Forcompatibilitywitholder

versionsofPythonssl_optionsmayalsobeadictionaryofkeyword

argumentsforthessl.wrap_socketmethod.::

ssl_ctx=ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)

ssl_ctx.load_cert_chain(os.path.join(data_dir,"mydomain.crt"),

os.path.join(data_dir,"mydomain.key"))

HTTPServer(applicaton,ssl_options=ssl_ctx)

HTTPServerinitializationfollowsoneofthreepatterns(the

initializationmethodsaredefinedontornado.tcpserver.TCPServer):

1.~tornado.tcpserver.TCPServer.listen:simplesingle-process::

server=HTTPServer(app)

server.listen(8888)

IOLoop.current().start()

Inmanycases,tornado.web.Application.listencanbeusedtoavoid

theneedtoexplicitlycreatetheHTTPServer.

2.~tornado.tcpserver.TCPServer.bind/~tornado.tcpserver.TCPServer.start:

simplemulti-process::

server=HTTPServer(app)

server.bind(8888)

server.start(0)#Forksmultiplesub-processes

IOLoop.current().start()

Whenusingthisinterface,an.IOLoopmust*not*bepassed

totheHTTPServerconstructor.~.TCPServer.startwillalwaysstart

theserveronthedefaultsingleton.IOLoop.

3.~tornado.tcpserver.TCPServer.add_sockets:advancedmulti-process::

sockets=tornado.netutil.bind_sockets(8888)

tornado.process.fork_processes(0)

server=HTTPServer(app)

server.add_sockets(sockets)

IOLoop.current().start()

The~.TCPServer.add_socketsinterfaceismorecomplicated,

butitcanbeusedwithtornado.process.fork_processesto

giveyoumoreflexibilityinwhentheforkhappens.

~.TCPServer.add_socketscanalsobeusedinsingle-process

serversifyouwanttocreateyourlisteningsocketsinsome

wayotherthantornado.netutil.bind_sockets.

..versionchanged::4.0

Addeddecompress_request,chunk_size,max_header_size,

idle_connection_timeout,body_timeout,max_body_size

arguments.Addedsupportfor.HTTPServerConnectionDelegate

instancesasrequest_callback.

..versionchanged::4.1

.HTTPServerConnectionDelegate.start_requestisnowcalledwith

twoarguments(server_conn,request_conn)(inaccordancewiththe

documentation)insteadofone(request_conn).

..versionchanged::4.2

HTTPServerisnowasubclassoftornado.util.Configurable.

"""

def__init__(self,*args,**kwargs):

#Ignoreargsto__init__;realinitializationbelongsin

#initializesincewe'reConfigurable.(there'ssomething

#weirdininitializationorderbetweenthisclass,

#Configurable,andTCPServersowecan'tleave__init__out

#completely)

pass

上面的注釋包含了:類的說明、例子、版本主要改動。

優秀的代碼風格:看到名字就能知道它是用來干什么的(顧名思義)、結構清晰、代碼風格統一(命名規則、格式)

這些優秀的特質都是為了:可讀性、容易理解。正如:代碼主要是給人看的,讓計算機運行是次要的

如果是在閱讀了不好的代碼,如果你心里在罵:“這代碼簡直是一坨

所以,自己的優秀的編碼風格也是成為一個合格的程序員必備的一項技能(面試要求會有這一項),通過閱讀代碼學習,模仿優秀的代碼風格,有助于自己寫出‘漂亮’、整潔的代碼。

以上內容為大家介紹了python閱讀代碼有助于養成優秀的代碼風格,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。

tags: python培訓
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
10年以上業內強師集結,手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
相關推薦HOT