{"id":452521,"date":"2025-03-21T09:00:18","date_gmt":"2025-03-21T09:00:18","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=452521"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=452521","title":{"rendered":"<span>\u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0432 Python: \u043a\u0430\u043a \u044f\u0437\u044b\u043a \u0437\u0430\u0431\u043e\u0442\u0438\u0442\u0441\u044f \u043e \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u0445 \u0437\u0430 \u0432\u0430\u0441 \u0438 \u043a\u043e\u0433\u0434\u0430 \u0441\u0442\u043e\u0438\u0442 \u0432\u043c\u0435\u0448\u0430\u0442\u044c\u0441\u044f<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/5be\/695\/49a\/5be69549abe97bf09505c5bc3c3aad88.png\" width=\"1078\" height=\"392\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5be\/695\/49a\/5be69549abe97bf09505c5bc3c3aad88.png\"\/><\/figure>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435, \u0447\u0442\u043e \u0432\u044b \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u043e\u0440, \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0434\u043e\u043c. \u0412\u044b \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b, \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0435 \u043a\u043e\u043c\u043d\u0430\u0442\u044b, \u043d\u043e&#8230; \u043a\u0442\u043e-\u0442\u043e \u0443\u0436\u0435 \u043f\u043e\u0434\u0432\u0435\u0437 \u043a\u0438\u0440\u043f\u0438\u0447\u0438, \u0446\u0435\u043c\u0435\u043d\u0442 \u0438 \u0434\u0430\u0436\u0435 \u0440\u0430\u0441\u0441\u0442\u0430\u0432\u0438\u043b \u043c\u0435\u0431\u0435\u043b\u044c. \u0417\u0432\u0443\u0447\u0438\u0442 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e? \u041f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a Python \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e: \u043e\u043d \u0431\u0435\u0440\u0435\u0442 \u043d\u0430 \u0441\u0435\u0431\u044f \u0440\u0443\u0442\u0438\u043d\u0443, \u0447\u0442\u043e\u0431\u044b \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u043b\u043e\u0433\u0438\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u0434\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0435\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u044d\u0442\u0430\u0436? \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u043a\u0430\u043a Python \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u043a\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0432\u0435\u0440\u0438\u0442\u044c\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u043a\u0435, \u0430 \u043a\u043e\u0433\u0434\u0430 \u0441\u0442\u043e\u0438\u0442 \u0432\u0437\u044f\u0442\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0432\u043e\u0438 \u0440\u0443\u043a\u0438.<\/p>\n<h3>1. \u0412\u044b \u0436\u0435 \u043d\u0435 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043a\u0438\u0440\u043f\u0438\u0447\u0438 \u043a\u043b\u0430\u0434\u0435\u0442\u0435? \u0417\u0430\u0447\u0435\u043c Python \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h3>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u043f\u0438\u0448\u0435\u0442\u0435\u00a0<code>x = [1, 2, 3]<\/code>, Python \u043d\u0435 \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u0430\u0441 \u0434\u0443\u043c\u0430\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u043e\u0434 \u0441\u043f\u0438\u0441\u043e\u043a. \u041e\u043d \u0441\u0430\u043c \u043d\u0430\u0445\u043e\u0434\u0438\u0442 &#171;\u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0435 \u043c\u0435\u0441\u0442\u043e&#187; \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u0443\u0435\u0442 \u0435\u0433\u043e \u0438 \u0441\u043b\u0435\u0434\u0438\u0442, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u044a\u0435\u043a\u0442 \u0436\u0438\u043b \u0440\u043e\u0432\u043d\u043e \u0441\u0442\u043e\u043b\u044c\u043a\u043e, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f. \u042d\u0442\u043e \u043a\u0430\u043a \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0431\u0440\u0438\u0433\u0430\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0432\u043e\u0437\u0438\u0442 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b, \u043d\u043e \u0438 \u0443\u0431\u0438\u0440\u0430\u0435\u0442 \u043c\u0443\u0441\u043e\u0440 \u043f\u043e\u0441\u043b\u0435 \u0440\u0435\u043c\u043e\u043d\u0442\u0430.<\/p>\n<h4>\u041a\u0430\u043a Python \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c?<\/h4>\n<p>\u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u043b\u0435\u0436\u0438\u0442\u00a0<strong>\u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u043f\u0430\u043c\u044f\u0442\u0438<\/strong>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u00a0<strong>private heaps<\/strong>\u00a0(\u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u043c\u0438 \u043a\u0443\u0447\u0430\u043c\u0438). \u041a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 Python \u2014 \u044d\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0430\u044f:<\/p>\n<ul>\n<li>\n<p>\u0422\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440,\u00a0int,\u00a0list).<\/p>\n<\/li>\n<li>\n<p>\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p>\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0441\u043f\u0438\u0441\u043a\u0430\u00a0<code>[1, 2, 3]<\/code>\u00a0\u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u043d\u043e \u0438 \u043f\u043e\u0434 \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e (\u0440\u0430\u0437\u043c\u0435\u0440, \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438). \u042d\u0442\u043e \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442 \u0443\u043f\u0430\u043a\u043e\u0432\u043a\u0443 \u0442\u043e\u0432\u0430\u0440\u0430 \u0432 \u043a\u043e\u0440\u043e\u0431\u043a\u0443: \u0441\u0430\u043c \u0442\u043e\u0432\u0430\u0440 + \u044d\u0442\u0438\u043a\u0435\u0442\u043a\u0438 \u0438 \u0430\u043c\u043e\u0440\u0442\u0438\u0437\u0430\u0446\u0438\u044f.<\/p>\n<p><strong>\u041f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e \u0432\u0430\u0436\u043d\u043e?<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c<\/strong>: \u041d\u0435\u0442 &#171;\u0432\u0438\u0441\u044f\u0447\u0438\u0445 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439&#187; (\u043a\u043e\u0433\u0434\u0430 \u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0430, \u043d\u043e \u0432\u044b \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u044b\u0442\u0430\u0435\u0442\u0435\u0441\u044c \u0435\u0451 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c).<\/p>\n<\/li>\n<li>\n<p><strong>\u0423\u0434\u043e\u0431\u0441\u0442\u0432\u043e<\/strong>: \u041d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u00a0<code>malloc<\/code>\u00a0\u0438\u00a0<code>free<\/code>, \u043a\u0430\u043a \u0432 C.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f<\/strong>: Python \u0437\u043d\u0430\u0435\u0442, \u043a\u0430\u043a \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435 \u0440\u0430\u0441\u043f\u043e\u0440\u044f\u0436\u0430\u0442\u044c\u0441\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u0435\u043b\u043a\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 (\u043e\u0442 -5 \u0434\u043e 256) \u043a\u044d\u0448\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<\/li>\n<\/ul>\n<h3>2. \u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a: \u0438\u0441\u0442\u043e\u0440\u0438\u044f \u043e \u0442\u043e\u043c, \u043a\u0430\u043a Python \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u0432\u0430\u0448\u0438 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0438<\/h3>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 Python \u2014 \u043a\u0430\u043a \u0432\u043e\u0437\u0434\u0443\u0448\u043d\u044b\u0439 \u0448\u0430\u0440\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0435\u0440\u0436\u0430\u0442 \u0437\u0430 \u043d\u0438\u0442\u043e\u0447\u043a\u0438. \u041f\u043e\u043a\u0430 \u043a\u0442\u043e-\u0442\u043e \u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0438\u0442\u044c (\u0435\u0441\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442), \u0448\u0430\u0440\u0438\u043a \u043b\u0435\u0442\u0430\u0435\u0442. \u041a\u043e\u0433\u0434\u0430 \u043d\u0438\u0442\u0438 \u043e\u0442\u043f\u0443\u0441\u043a\u0430\u044e\u0442 \u2014 \u043e\u043d \u0443\u043b\u0435\u0442\u0430\u0435\u0442 (\u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f). \u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u00a0<strong>\u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a<\/strong>.<\/p>\n<h4>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c?<\/h4>\n<p>\u0412 CPython (\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Python) \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u043e\u043b\u0435\u00a0ob_refcnt, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0441\u044b\u043b\u043e\u043a. \u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u043d\u0430\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0435 \u0435\u0451 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0435, \u044d\u0442\u043e \u043f\u043e\u043b\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440:<\/strong><\/p>\n<pre><code class=\"python\">a = [1, 2, 3]  # ob_refcnt = 1 b = a          # ob_refcnt = 2 del a          # ob_refcnt = 1 b.append(4)    # \u0421\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u2014 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 b = None       # ob_refcnt = 0 \u2192 \u043e\u0431\u044a\u0435\u043a\u0442 \u0443\u0434\u0430\u043b\u0435\u043d<\/code><\/pre>\n<p><strong>\u041d\u043e \u0435\u0441\u0442\u044c \u043d\u044e\u0430\u043d\u0441\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u0421\u0442\u0440\u043e\u043a\u0438 \u0438 \u0438\u043d\u0442\u0435\u0440\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (interning)<\/strong>: Python \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b), \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>\u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043d\u0430 C<\/strong>: \u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437\u00a0<code>Py_INCREF<\/code>\u00a0\u0438\u00a0<code>Py_DECREF<\/code>. \u041e\u0448\u0438\u0431\u043a\u0438 \u0437\u0434\u0435\u0441\u044c \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u0443\u0442\u0435\u0447\u043a\u0430\u043c \u0438\u043b\u0438 \u043a\u0440\u0430\u0448\u0430\u043c.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435\u00a0sys.getrefcount(), \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a (\u043d\u043e \u0443\u0447\u0442\u0438\u0442\u0435, \u0447\u0442\u043e \u0441\u0430\u043c \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0430 1).<\/p>\n<h3>3. \u0421\u0431\u043e\u0440\u0449\u0438\u043a \u043c\u0443\u0441\u043e\u0440\u0430: \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442 &#171;\u0437\u0430\u0431\u044b\u0442\u044b\u0435&#187; \u043e\u0431\u044a\u0435\u043a\u0442\u044b<\/h3>\n<p><strong>\u0421\u0431\u043e\u0440\u0449\u0438\u043a \u043c\u0443\u0441\u043e\u0440\u0430 (Garbage Collector, GC)<\/strong> \u2014 \u044d\u0442\u043e \u043a\u0430\u043a \u0443\u0431\u043e\u0440\u0449\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\u0438\u0442 &#171;\u043a\u043e\u043c\u043d\u0430\u0442\u044b&#187; \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0438\u0449\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0431\u0435\u0437 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u0441\u0441\u044b\u043b\u043e\u043a. \u041d\u043e \u043a\u0430\u043a \u043e\u043d \u043d\u0430\u0445\u043e\u0434\u0438\u0442 \u0442\u0435 \u0441\u0430\u043c\u044b\u0435 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438?<\/p>\n<h4>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0439 (Generational GC)<\/h4>\n<p>Python \u0434\u0435\u043b\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0430 \u0442\u0440\u0438 \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\n<p><strong>\u041f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0435 0<\/strong>: \u041d\u043e\u0432\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0435 1<\/strong>: \u041e\u0431\u044a\u0435\u043a\u0442\u044b, \u043f\u0435\u0440\u0435\u0436\u0438\u0432\u0448\u0438\u0435 \u043e\u0434\u043d\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0435 2<\/strong>: &#171;\u0414\u043e\u043b\u0433\u043e\u0436\u0438\u0442\u0435\u043b\u0438&#187;. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u0440\u0435\u0436\u0435.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a?<\/strong><br \/>\u0418\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442, \u0447\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 &#171;\u0443\u043c\u0438\u0440\u0430\u044e\u0442&#187; \u043c\u043e\u043b\u043e\u0434\u044b\u043c\u0438. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u044f \u043c\u043e\u043b\u043e\u0434\u043e\u0435 \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0435 \u0447\u0430\u0449\u0435, Python \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442 \u0440\u0435\u0441\u0443\u0440\u0441\u044b.<\/p>\n<p><strong>\u041a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c GC? <\/strong>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u0440\u043e\u0433\u0430\u043c\u0438 \u0441\u0431\u043e\u0440\u043a\u0438 \u0447\u0435\u0440\u0435\u0437 \u043c\u043e\u0434\u0443\u043b\u044c\u00a0<code>gc<\/code>:<\/p>\n<pre><code class=\"python\">import gc  gc.set_threshold(700, 10, 10)  # \u041f\u043e\u0440\u043e\u0433\u0438 \u0434\u043b\u044f \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0439 0, 1, 2<\/code><\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438:<\/strong><\/p>\n<pre><code class=\"python\">class Node:     def __init__(self):         self.parent = None  # \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0443\u0437\u043b\u044b-\u0431\u043b\u0438\u0437\u043d\u0435\u0446\u044b child = Node() parent = Node()  # \u0417\u0430\u043c\u044b\u043a\u0430\u0435\u043c \u0441\u0441\u044b\u043b\u043a\u0438 child.parent = parent parent.child = child  # \u0426\u0438\u043a\u043b!  # \u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 child = None parent = None  # \u0422\u0435\u043f\u0435\u0440\u044c GC \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442, \u0447\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0435\u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u044b, \u0438 \u0443\u0434\u0430\u043b\u0438\u0442 \u0438\u0445<\/code><\/pre>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0415\u0441\u043b\u0438 \u0432\u0430\u0448 \u043a\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u0441\u044b\u043b\u043e\u043a, \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0439\u0442\u0435\u00a0<code>gc.collect()<\/code>\u00a0\u0432\u0440\u0443\u0447\u043d\u0443\u044e.<\/p>\n<h3>4. \u041a\u043e\u0433\u0434\u0430 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u043a\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e: \u043a\u0430\u043a \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e<\/h3>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 &#171;\u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0431\u0440\u0438\u0433\u0430\u0434\u0430&#187; Python \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435\u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u0435 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<\/p>\n<h4>4.1.\u00a0_ _slots_ _: \u043a\u043e\u0433\u0434\u0430 \u0441\u043b\u043e\u0432\u0430\u0440\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0442\u044f\u0436\u0435\u043b\u044b<\/h4>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 Python \u0445\u0440\u0430\u043d\u0438\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u00a0<strong>dict<\/strong>, \u0447\u0442\u043e \u0433\u0438\u0431\u043a\u043e, \u043d\u043e \u043d\u0435\u044d\u043a\u043e\u043d\u043e\u043c\u043d\u043e.<\/p>\n<p><code><strong>_ _slots_ _<\/strong><\/code> \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u043d\u0430 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432, \u044d\u043a\u043e\u043d\u043e\u043c\u044f \u0434\u043e 40% \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p><strong>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435:<\/strong><\/p>\n<pre><code class=\"python\">class User:     def __init__(self, name, age):         self.name = name         self.age = age  class SlotUser:     __slots__ = ['name', 'age']     def __init__(self, name, age):         self.name = name         self.age = age  # \u041f\u0430\u043c\u044f\u0442\u044c \u0434\u043b\u044f 100_000 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432: # \u041e\u0431\u044b\u0447\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441: ~15 \u041c\u0411 # \u041a\u043b\u0430\u0441\u0441 \u0441 __slots__: ~8 \u041c\u0411<\/code><\/pre>\n<p><strong>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041d\u0435\u043b\u044c\u0437\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e\u0441\u0442\u0438: \u0435\u0441\u043b\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0438\u043c\u0435\u0435\u0442\u00a0<code><strong>_ _slots_ _<\/strong><\/code>, \u043f\u043e\u0442\u043e\u043c\u043e\u043a \u0434\u043e\u043b\u0436\u0435\u043d \u0435\u0433\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435\u00a0<code><strong>_ _slots_ _<\/strong><\/code>\u00a0\u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430\u043c\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443\u0437\u043b\u044b \u0434\u0435\u0440\u0435\u0432\u0430, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430).<\/p>\n<h4>4.2. \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b: \u043f\u0430\u043c\u044f\u0442\u044c &#171;\u043d\u0430 \u043f\u043e\u0442\u043e\u043a\u0435&#187;<\/h4>\n<p>\u0427\u0442\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u0447\u0435\u0440\u0435\u0437\u00a0<code><strong>read()<\/strong><\/code>\u00a0\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0432\u0441\u0451 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c. \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0447\u0430\u0441\u0442\u044f\u043c:<\/p>\n<pre><code class=\"python\"># \u041f\u043b\u043e\u0445\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0444\u0430\u0439\u043b\u043e\u0432: with open(\"huge.log\") as f:     lines = f.readlines()  # \u0412\u0435\u0441\u044c \u0444\u0430\u0439\u043b \u0432 \u043f\u0430\u043c\u044f\u0442\u0438!  # \u0425\u043e\u0440\u043e\u0448\u043e: def read_lines(filename):     with open(filename) as f:         for line in f:             yield line  # \u041f\u043e \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438  for line in read_lines(\"huge.log\"):     process(line)<\/code><\/pre>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 (CSV, JSON, \u043b\u043e\u0433\u0438).<\/p>\n<h4>4.3. \u041c\u0430\u0441\u0441\u0438\u0432\u044b \u0438\u00a0numpy: \u043a\u043e\u0433\u0434\u0430 \u0441\u043f\u0438\u0441\u043a\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0435<\/h4>\n<p>\u0414\u043b\u044f \u0447\u0438\u0441\u0435\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043c\u043e\u0434\u0443\u043b\u044c\u00a0<code><strong>array<\/strong><\/code>\u00a0\u0438\u043b\u0438\u00a0<code><strong>numpy<\/strong><\/code>:<\/p>\n<pre><code class=\"python\">import array  # \u041e\u0431\u044b\u0447\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a: numbers = [1, 2, 3, 4, 5]  # \u041a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u2014 \u043e\u0431\u044a\u0435\u043a\u0442 int (~28 \u0431\u0430\u0439\u0442)  # \u041c\u0430\u0441\u0441\u0438\u0432: arr = array.array('i', [1, 2, 3, 4, 5])  # \u041a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u2014 4 \u0431\u0430\u0439\u0442\u0430<\/code><\/pre>\n<p><strong>\u041f\u043b\u044e\u0441\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p>\u042d\u043a\u043e\u043d\u043e\u043c\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0411\u044b\u0441\u0442\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041c\u0438\u043d\u0443\u0441\u044b:<\/strong>\u00a0\u041e\u0434\u043d\u043e\u0442\u0438\u043f\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<h3>5. \u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b: \u043a\u0430\u043a \u0438\u0441\u043a\u0430\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438<\/h3>\n<p><strong>\u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439:<\/strong>\u00a0\u0412\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 &#171;\u0436\u0440\u0430\u0442\u044c&#187; \u043f\u0430\u043c\u044f\u0442\u044c. \u041a\u0430\u043a \u043d\u0430\u0439\u0442\u0438 \u0432\u0438\u043d\u043e\u0432\u043d\u0438\u043a\u0430?<\/p>\n<h4>5.1.\u00a0tracemalloc: \u0441\u043b\u0435\u0436\u043a\u0430 \u0437\u0430 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h4>\n<pre><code class=\"python\">import tracemalloc  tracemalloc.start()  # \u041a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0443 data = [x for x in range(10_000)]  snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno')  for stat in top_stats[:3]:  # \u0422\u043e\u043f-3 \"\u043f\u043e\u0434\u043e\u0437\u0440\u0435\u0432\u0430\u0435\u043c\u044b\u0445\"     print(f\"{stat.count} \u0431\u043b\u043e\u043a\u043e\u0432: {stat.size \/ 1024} \u041a\u0411\")     print(stat.traceback.format()[-1])  # \u0413\u0434\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0430 \u043f\u0430\u043c\u044f\u0442\u044c<\/code><\/pre>\n<h4>5.2.\u00a0objgraph: \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/h4>\n<pre><code class=\"python\">import objgraph  # \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0443\u0442\u0435\u0447\u043a\u0443 cache = [] def leak():     cache.append([1, 2, 3])  for _ in range(100):     leak()  # \u0410\u043d\u0430\u043b\u0438\u0437 objgraph.show_most_common_types(limit=5)  # \u041a\u0430\u043a\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043f\u043b\u043e\u0434\u044f\u0442\u0441\u044f? objgraph.show_backrefs([cache], filename=\"graph.png\")  # \u0413\u0440\u0430\u0444 \u0441\u0432\u044f\u0437\u0435\u0439<\/code><\/pre>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0415\u0441\u043b\u0438 \u0432\u0438\u0434\u0438\u0442\u0435 \u0440\u0430\u0441\u0442\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\u00a0dict\u00a0\u0438\u043b\u0438\u00a0list, \u043f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435, \u043d\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0435 \u043b\u0438 \u0432\u044b \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043a\u044d\u0448 \u0431\u0435\u0437 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439.  <\/p>\n<h3>6. \u0420\u0435\u0446\u0435\u043f\u0442\u044b \u0434\u043b\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h3>\n<h4>6.1. \u041a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0435\u0437 \u0443\u0442\u0435\u0447\u0435\u043a: \u0441\u043b\u0430\u0431\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438<\/h4>\n<p>\u041e\u0431\u044b\u0447\u043d\u044b\u0439 \u043a\u044d\u0448 \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0438\u043b\u044c\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438, \u043d\u0435 \u0434\u0430\u0432\u0430\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c \u0443\u0434\u0430\u043b\u044f\u0442\u044c\u0441\u044f.\u00a0<\/p>\n<p><code><strong>WeakValueDictionary<\/strong><\/code>\u00a0\u0440\u0435\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443:<\/p>\n<pre><code class=\"python\">import weakref  class Cache:     def __init__(self):         self._data = weakref.WeakValueDictionary()      def get(self, key):         return self._data.get(key)      def set(self, key, value):         self._data[key] = value  # \u041e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u043a\u044d\u0448\u0435 \u0443\u0434\u0430\u043b\u044f\u044e\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430 \u043d\u0438\u0445 \u043d\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0441\u044b\u043b\u043e\u043a<\/code><\/pre>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0441\u043b\u0430\u0431\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u0434\u043b\u044f \u043a\u044d\u0448\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u043b\u0438\u044f\u0442\u044c \u043d\u0430 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u044d\u0448 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439).<\/p>\n<h4>6.2. \u041f\u0443\u043b\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432:\u00a0tuple\u00a0vs\u00a0list<\/h4>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0442\u0438\u043f\u044b (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440,\u00a0tuple) \u0434\u043b\u044f \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"python\"># \u041f\u043b\u043e\u0445\u043e: points = [ [x, y] for x, y in coordinates ]  # \u041a\u0430\u0436\u0434\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u2014 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442  # \u041b\u0443\u0447\u0448\u0435: points = [ (x, y) for x, y in coordinates ]  # \u041a\u043e\u0440\u0442\u0435\u0436\u0438 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438<\/code><\/pre>\n<h4>6.3. \u041b\u0435\u043d\u0438\u0432\u044b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u00a0functools.lru_cache<\/h4>\n<p>\u041a\u044d\u0448\u0438\u0440\u0443\u0439\u0442\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">from functools import lru_cache  @lru_cache(maxsize=1000)  # \u041d\u0435 \u0431\u043e\u043b\u0435\u0435 1000 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 def calculate(x):     return x ** 2<\/code><\/pre>\n<h3>7. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435: \u0434\u043e\u0432\u0435\u0440\u044f\u0439, \u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0439<\/h3>\n<p>Python \u2014 \u043a\u0430\u043a \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0435\u0440\u0435\u0442 \u043d\u0430 \u0441\u0435\u0431\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e. \u041d\u043e \u0434\u0430\u0436\u0435 \u043b\u0443\u0447\u0448\u0438\u0435 \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a\u0438 \u0438\u043d\u043e\u0433\u0434\u0430 \u043e\u0448\u0438\u0431\u0430\u044e\u0442\u0441\u044f. \u0412\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043a\u0430\u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430:<\/p>\n<ul>\n<li>\n<p><strong>\u041f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u044b<\/strong>\u00a0\u0440\u0430\u0431\u043e\u0442\u044b \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0441\u0441\u044b\u043b\u043e\u043a \u0438 GC.<\/p>\n<\/li>\n<li>\n<p><strong>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b<\/strong>\u00a0(tracemalloc,\u00a0objgraph) \u0434\u043b\u044f \u043e\u0442\u043b\u0430\u0434\u043a\u0438.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b<\/strong>\u00a0(__slots__, \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b, \u0441\u043b\u0430\u0431\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438) \u0432 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u044b\u0445 \u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u0435\u0441\u0442\u0430\u0445.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u043c\u043d\u0438\u0442\u0435: \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0440\u0430\u0434\u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u0430. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0438\u0448\u0438\u0442\u0435 \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u0439 \u043a\u043e\u0434, \u0430 \u0437\u0430\u0442\u0435\u043c &#171;\u0442\u044e\u043d\u0438\u0433\u0443\u0439\u0442\u0435&#187; \u0435\u0433\u043e, \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0432\u0438\u0434\u0438\u0442\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b. <\/p>\n<p>\u041a\u0430\u043a \u0433\u043e\u0432\u043e\u0440\u0438\u043b \u0414\u043e\u043d\u0430\u043b\u044c\u0434 \u041a\u043d\u0443\u0442:<\/p>\n<blockquote>\n<p>\u00a0&#171;<em>\u041f\u0440\u0435\u0436\u0434\u0435\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u2014 \u043a\u043e\u0440\u0435\u043d\u044c \u0432\u0441\u0435\u0445 \u0437\u043e\u043b<\/em>&#171;.<\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/892922\/\"> https:\/\/habr.com\/ru\/articles\/892922\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435, \u0447\u0442\u043e \u0432\u044b \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u043e\u0440, \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0434\u043e\u043c. \u0412\u044b \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b, \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0435 \u043a\u043e\u043c\u043d\u0430\u0442\u044b, \u043d\u043e&#8230; \u043a\u0442\u043e-\u0442\u043e \u0443\u0436\u0435 \u043f\u043e\u0434\u0432\u0435\u0437 \u043a\u0438\u0440\u043f\u0438\u0447\u0438, \u0446\u0435\u043c\u0435\u043d\u0442 \u0438 \u0434\u0430\u0436\u0435 \u0440\u0430\u0441\u0441\u0442\u0430\u0432\u0438\u043b \u043c\u0435\u0431\u0435\u043b\u044c. \u0417\u0432\u0443\u0447\u0438\u0442 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e? \u041f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a Python \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e: \u043e\u043d \u0431\u0435\u0440\u0435\u0442 \u043d\u0430 \u0441\u0435\u0431\u044f \u0440\u0443\u0442\u0438\u043d\u0443, \u0447\u0442\u043e\u0431\u044b \u0432\u044b \u043c\u043e\u0433\u043b\u0438 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u043b\u043e\u0433\u0438\u043a\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u0434\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0435\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u044d\u0442\u0430\u0436? \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u043a\u0430\u043a Python \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u043a\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0432\u0435\u0440\u0438\u0442\u044c\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u043a\u0435, \u0430 \u043a\u043e\u0433\u0434\u0430 \u0441\u0442\u043e\u0438\u0442 \u0432\u0437\u044f\u0442\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0432\u043e\u0438 \u0440\u0443\u043a\u0438.<\/p>\n<h3>1. \u0412\u044b \u0436\u0435 \u043d\u0435 \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u043a\u0438\u0440\u043f\u0438\u0447\u0438 \u043a\u043b\u0430\u0434\u0435\u0442\u0435? \u0417\u0430\u0447\u0435\u043c Python \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h3>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u043f\u0438\u0448\u0435\u0442\u0435\u00a0<code>x = [1, 2, 3]<\/code>, Python \u043d\u0435 \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u0430\u0441 \u0434\u0443\u043c\u0430\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u043e\u0434 \u0441\u043f\u0438\u0441\u043e\u043a. \u041e\u043d \u0441\u0430\u043c \u043d\u0430\u0445\u043e\u0434\u0438\u0442 &#171;\u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0435 \u043c\u0435\u0441\u0442\u043e&#187; \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u0443\u0435\u0442 \u0435\u0433\u043e \u0438 \u0441\u043b\u0435\u0434\u0438\u0442, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u044a\u0435\u043a\u0442 \u0436\u0438\u043b \u0440\u043e\u0432\u043d\u043e \u0441\u0442\u043e\u043b\u044c\u043a\u043e, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f. \u042d\u0442\u043e \u043a\u0430\u043a \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0431\u0440\u0438\u0433\u0430\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0432\u043e\u0437\u0438\u0442 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b, \u043d\u043e \u0438 \u0443\u0431\u0438\u0440\u0430\u0435\u0442 \u043c\u0443\u0441\u043e\u0440 \u043f\u043e\u0441\u043b\u0435 \u0440\u0435\u043c\u043e\u043d\u0442\u0430.<\/p>\n<h4>\u041a\u0430\u043a Python \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c?<\/h4>\n<p>\u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u043b\u0435\u0436\u0438\u0442\u00a0<strong>\u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u043f\u0430\u043c\u044f\u0442\u0438<\/strong>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u00a0<strong>private heaps<\/strong>\u00a0(\u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u043c\u0438 \u043a\u0443\u0447\u0430\u043c\u0438). \u041a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 Python \u2014 \u044d\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0430\u044f:<\/p>\n<ul>\n<li>\n<p>\u0422\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440,\u00a0int,\u00a0list).<\/p>\n<\/li>\n<li>\n<p>\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p>\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0441\u043f\u0438\u0441\u043a\u0430\u00a0<code>[1, 2, 3]<\/code>\u00a0\u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u043d\u043e \u0438 \u043f\u043e\u0434 \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e (\u0440\u0430\u0437\u043c\u0435\u0440, \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438). \u042d\u0442\u043e \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442 \u0443\u043f\u0430\u043a\u043e\u0432\u043a\u0443 \u0442\u043e\u0432\u0430\u0440\u0430 \u0432 \u043a\u043e\u0440\u043e\u0431\u043a\u0443: \u0441\u0430\u043c \u0442\u043e\u0432\u0430\u0440 + \u044d\u0442\u0438\u043a\u0435\u0442\u043a\u0438 \u0438 \u0430\u043c\u043e\u0440\u0442\u0438\u0437\u0430\u0446\u0438\u044f.<\/p>\n<p><strong>\u041f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e \u0432\u0430\u0436\u043d\u043e?<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c<\/strong>: \u041d\u0435\u0442 &#171;\u0432\u0438\u0441\u044f\u0447\u0438\u0445 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439&#187; (\u043a\u043e\u0433\u0434\u0430 \u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0430, \u043d\u043e \u0432\u044b \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u044b\u0442\u0430\u0435\u0442\u0435\u0441\u044c \u0435\u0451 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c).<\/p>\n<\/li>\n<li>\n<p><strong>\u0423\u0434\u043e\u0431\u0441\u0442\u0432\u043e<\/strong>: \u041d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u00a0<code>malloc<\/code>\u00a0\u0438\u00a0<code>free<\/code>, \u043a\u0430\u043a \u0432 C.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f<\/strong>: Python \u0437\u043d\u0430\u0435\u0442, \u043a\u0430\u043a \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435 \u0440\u0430\u0441\u043f\u043e\u0440\u044f\u0436\u0430\u0442\u044c\u0441\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u0435\u043b\u043a\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 (\u043e\u0442 -5 \u0434\u043e 256) \u043a\u044d\u0448\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<\/li>\n<\/ul>\n<h3>2. \u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a: \u0438\u0441\u0442\u043e\u0440\u0438\u044f \u043e \u0442\u043e\u043c, \u043a\u0430\u043a Python \u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u0432\u0430\u0448\u0438 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0438<\/h3>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 Python \u2014 \u043a\u0430\u043a \u0432\u043e\u0437\u0434\u0443\u0448\u043d\u044b\u0439 \u0448\u0430\u0440\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0435\u0440\u0436\u0430\u0442 \u0437\u0430 \u043d\u0438\u0442\u043e\u0447\u043a\u0438. \u041f\u043e\u043a\u0430 \u043a\u0442\u043e-\u0442\u043e \u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0438\u0442\u044c (\u0435\u0441\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442), \u0448\u0430\u0440\u0438\u043a \u043b\u0435\u0442\u0430\u0435\u0442. \u041a\u043e\u0433\u0434\u0430 \u043d\u0438\u0442\u0438 \u043e\u0442\u043f\u0443\u0441\u043a\u0430\u044e\u0442 \u2014 \u043e\u043d \u0443\u043b\u0435\u0442\u0430\u0435\u0442 (\u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f). \u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u00a0<strong>\u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a<\/strong>.<\/p>\n<h4>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c?<\/h4>\n<p>\u0412 CPython (\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Python) \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u043e\u043b\u0435\u00a0ob_refcnt, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0441\u044b\u043b\u043e\u043a. \u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u043d\u0430\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0435 \u0435\u0451 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0435, \u044d\u0442\u043e \u043f\u043e\u043b\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440:<\/strong><\/p>\n<pre><code class=\"python\">a = [1, 2, 3]  # ob_refcnt = 1 b = a          # ob_refcnt = 2 del a          # ob_refcnt = 1 b.append(4)    # \u0421\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u2014 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 b = None       # ob_refcnt = 0 \u2192 \u043e\u0431\u044a\u0435\u043a\u0442 \u0443\u0434\u0430\u043b\u0435\u043d<\/code><\/pre>\n<p><strong>\u041d\u043e \u0435\u0441\u0442\u044c \u043d\u044e\u0430\u043d\u0441\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u0421\u0442\u0440\u043e\u043a\u0438 \u0438 \u0438\u043d\u0442\u0435\u0440\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (interning)<\/strong>: Python \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b), \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>\u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043d\u0430 C<\/strong>: \u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437\u00a0<code>Py_INCREF<\/code>\u00a0\u0438\u00a0<code>Py_DECREF<\/code>. \u041e\u0448\u0438\u0431\u043a\u0438 \u0437\u0434\u0435\u0441\u044c \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u0443\u0442\u0435\u0447\u043a\u0430\u043c \u0438\u043b\u0438 \u043a\u0440\u0430\u0448\u0430\u043c.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435\u00a0sys.getrefcount(), \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0441\u0441\u044b\u043b\u043e\u043a (\u043d\u043e \u0443\u0447\u0442\u0438\u0442\u0435, \u0447\u0442\u043e \u0441\u0430\u043c \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442 \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0430 1).<\/p>\n<h3>3. \u0421\u0431\u043e\u0440\u0449\u0438\u043a \u043c\u0443\u0441\u043e\u0440\u0430: \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442 &#171;\u0437\u0430\u0431\u044b\u0442\u044b\u0435&#187; \u043e\u0431\u044a\u0435\u043a\u0442\u044b<\/h3>\n<p><strong>\u0421\u0431\u043e\u0440\u0449\u0438\u043a \u043c\u0443\u0441\u043e\u0440\u0430 (Garbage Collector, GC)<\/strong> \u2014 \u044d\u0442\u043e \u043a\u0430\u043a \u0443\u0431\u043e\u0440\u0449\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\u0438\u0442 &#171;\u043a\u043e\u043c\u043d\u0430\u0442\u044b&#187; \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0438\u0449\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0431\u0435\u0437 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u0441\u0441\u044b\u043b\u043e\u043a. \u041d\u043e \u043a\u0430\u043a \u043e\u043d \u043d\u0430\u0445\u043e\u0434\u0438\u0442 \u0442\u0435 \u0441\u0430\u043c\u044b\u0435 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438?<\/p>\n<h4>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0439 (Generational GC)<\/h4>\n<p>Python \u0434\u0435\u043b\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0430 \u0442\u0440\u0438 \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\n<p><strong>\u041f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0435 0<\/strong>: \u041d\u043e\u0432\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0435 1<\/strong>: \u041e\u0431\u044a\u0435\u043a\u0442\u044b, \u043f\u0435\u0440\u0435\u0436\u0438\u0432\u0448\u0438\u0435 \u043e\u0434\u043d\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0435 2<\/strong>: &#171;\u0414\u043e\u043b\u0433\u043e\u0436\u0438\u0442\u0435\u043b\u0438&#187;. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u0440\u0435\u0436\u0435.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a?<\/strong><br \/>\u0418\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442, \u0447\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 &#171;\u0443\u043c\u0438\u0440\u0430\u044e\u0442&#187; \u043c\u043e\u043b\u043e\u0434\u044b\u043c\u0438. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u044f \u043c\u043e\u043b\u043e\u0434\u043e\u0435 \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0435 \u0447\u0430\u0449\u0435, Python \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442 \u0440\u0435\u0441\u0443\u0440\u0441\u044b.<\/p>\n<p><strong>\u041a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c GC? <\/strong>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u0440\u043e\u0433\u0430\u043c\u0438 \u0441\u0431\u043e\u0440\u043a\u0438 \u0447\u0435\u0440\u0435\u0437 \u043c\u043e\u0434\u0443\u043b\u044c\u00a0<code>gc<\/code>:<\/p>\n<pre><code class=\"python\">import gc  gc.set_threshold(700, 10, 10)  # \u041f\u043e\u0440\u043e\u0433\u0438 \u0434\u043b\u044f \u043f\u043e\u043a\u043e\u043b\u0435\u043d\u0438\u0439 0, 1, 2<\/code><\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u0441\u044b\u043b\u043a\u0438:<\/strong><\/p>\n<pre><code class=\"python\">class Node:     def __init__(self):         self.parent = None  # \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0443\u0437\u043b\u044b-\u0431\u043b\u0438\u0437\u043d\u0435\u0446\u044b child = Node() parent = Node()  # \u0417\u0430\u043c\u044b\u043a\u0430\u0435\u043c \u0441\u0441\u044b\u043b\u043a\u0438 child.parent = parent parent.child = child  # \u0426\u0438\u043a\u043b!  # \u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 child = None parent = None  # \u0422\u0435\u043f\u0435\u0440\u044c GC \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442, \u0447\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043d\u0435\u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u044b, \u0438 \u0443\u0434\u0430\u043b\u0438\u0442 \u0438\u0445<\/code><\/pre>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0415\u0441\u043b\u0438 \u0432\u0430\u0448 \u043a\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u0441\u044b\u043b\u043e\u043a, \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0439\u0442\u0435\u00a0<code>gc.collect()<\/code>\u00a0\u0432\u0440\u0443\u0447\u043d\u0443\u044e.<\/p>\n<h3>4. \u041a\u043e\u0433\u0434\u0430 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u043a\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e: \u043a\u0430\u043a \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e<\/h3>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 &#171;\u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0431\u0440\u0438\u0433\u0430\u0434\u0430&#187; Python \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435\u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438\u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442\u0435 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438.<\/p>\n<h4>4.1.\u00a0_ _slots_ _: \u043a\u043e\u0433\u0434\u0430 \u0441\u043b\u043e\u0432\u0430\u0440\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0442\u044f\u0436\u0435\u043b\u044b<\/h4>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 Python \u0445\u0440\u0430\u043d\u0438\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u00a0<strong>dict<\/strong>, \u0447\u0442\u043e \u0433\u0438\u0431\u043a\u043e, \u043d\u043e \u043d\u0435\u044d\u043a\u043e\u043d\u043e\u043c\u043d\u043e.<\/p>\n<p><code><strong>_ _slots_ _<\/strong><\/code> \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u043d\u0430 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432, \u044d\u043a\u043e\u043d\u043e\u043c\u044f \u0434\u043e 40% \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p><strong>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435:<\/strong><\/p>\n<pre><code class=\"python\">class User:     def __init__(self, name, age):         self.name = name         self.age = age  class SlotUser:     __slots__ = ['name', 'age']     def __init__(self, name, age):         self.name = name         self.age = age  # \u041f\u0430\u043c\u044f\u0442\u044c \u0434\u043b\u044f 100_000 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432: # \u041e\u0431\u044b\u0447\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441: ~15 \u041c\u0411 # \u041a\u043b\u0430\u0441\u0441 \u0441 __slots__: ~8 \u041c\u0411<\/code><\/pre>\n<p><strong>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f:<\/strong><\/p>\n<ul>\n<li>\n<p>\u041d\u0435\u043b\u044c\u0437\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e\u0441\u0442\u0438: \u0435\u0441\u043b\u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0438\u043c\u0435\u0435\u0442\u00a0<code><strong>_ _slots_ _<\/strong><\/code>, \u043f\u043e\u0442\u043e\u043c\u043e\u043a \u0434\u043e\u043b\u0436\u0435\u043d \u0435\u0433\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435\u00a0<code><strong>_ _slots_ _<\/strong><\/code>\u00a0\u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430\u043c\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443\u0437\u043b\u044b \u0434\u0435\u0440\u0435\u0432\u0430, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430).<\/p>\n<h4>4.2. \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b: \u043f\u0430\u043c\u044f\u0442\u044c &#171;\u043d\u0430 \u043f\u043e\u0442\u043e\u043a\u0435&#187;<\/h4>\n<p>\u0427\u0442\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430 \u0447\u0435\u0440\u0435\u0437\u00a0<code><strong>read()<\/strong><\/code>\u00a0\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0432\u0441\u0451 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c. \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0447\u0430\u0441\u0442\u044f\u043c:<\/p>\n<pre><code class=\"python\"># \u041f\u043b\u043e\u0445\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0444\u0430\u0439\u043b\u043e\u0432: with open(\"huge.log\") as f:     lines = f.readlines()  # \u0412\u0435\u0441\u044c \u0444\u0430\u0439\u043b \u0432 \u043f\u0430\u043c\u044f\u0442\u0438!  # \u0425\u043e\u0440\u043e\u0448\u043e: def read_lines(filename):     with open(filename) as f:         for line in f:             yield line  # \u041f\u043e \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438  for line in read_lines(\"huge.log\"):     process(line)<\/code><\/pre>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 (CSV, JSON, \u043b\u043e\u0433\u0438).<\/p>\n<h4>4.3. \u041c\u0430\u0441\u0441\u0438\u0432\u044b \u0438\u00a0numpy: \u043a\u043e\u0433\u0434\u0430 \u0441\u043f\u0438\u0441\u043a\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0435<\/h4>\n<p>\u0414\u043b\u044f \u0447\u0438\u0441\u0435\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043c\u043e\u0434\u0443\u043b\u044c\u00a0<code><strong>array<\/strong><\/code>\u00a0\u0438\u043b\u0438\u00a0<code><strong>numpy<\/strong><\/code>:<\/p>\n<pre><code class=\"python\">import array  # \u041e\u0431\u044b\u0447\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a: numbers = [1, 2, 3, 4, 5]  # \u041a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u2014 \u043e\u0431\u044a\u0435\u043a\u0442 int (~28 \u0431\u0430\u0439\u0442)  # \u041c\u0430\u0441\u0441\u0438\u0432: arr = array.array('i', [1, 2, 3, 4, 5])  # \u041a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u2014 4 \u0431\u0430\u0439\u0442\u0430<\/code><\/pre>\n<p><strong>\u041f\u043b\u044e\u0441\u044b:<\/strong><\/p>\n<ul>\n<li>\n<p>\u042d\u043a\u043e\u043d\u043e\u043c\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0411\u044b\u0441\u0442\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041c\u0438\u043d\u0443\u0441\u044b:<\/strong>\u00a0\u041e\u0434\u043d\u043e\u0442\u0438\u043f\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.<\/p>\n<h3>5. \u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b: \u043a\u0430\u043a \u0438\u0441\u043a\u0430\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438<\/h3>\n<p><strong>\u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439:<\/strong>\u00a0\u0412\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 &#171;\u0436\u0440\u0430\u0442\u044c&#187; \u043f\u0430\u043c\u044f\u0442\u044c. \u041a\u0430\u043a \u043d\u0430\u0439\u0442\u0438 \u0432\u0438\u043d\u043e\u0432\u043d\u0438\u043a\u0430?<\/p>\n<h4>5.1.\u00a0tracemalloc: \u0441\u043b\u0435\u0436\u043a\u0430 \u0437\u0430 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h4>\n<pre><code class=\"python\">import tracemalloc  tracemalloc.start()  # \u041a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0443 data = [x for x in range(10_000)]  snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno')  for stat in top_stats[:3]:  # \u0422\u043e\u043f-3 \"\u043f\u043e\u0434\u043e\u0437\u0440\u0435\u0432\u0430\u0435\u043c\u044b\u0445\"     print(f\"{stat.count} \u0431\u043b\u043e\u043a\u043e\u0432: {stat.size \/ 1024} \u041a\u0411\")     print(stat.traceback.format()[-1])  # \u0413\u0434\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0430 \u043f\u0430\u043c\u044f\u0442\u044c<\/code><\/pre>\n<h4>5.2.\u00a0objgraph: \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/h4>\n<pre><code class=\"python\">import objgraph  # \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0443\u0442\u0435\u0447\u043a\u0443 cache = [] def leak():     cache.append([1, 2, 3])  for _ in range(100):     leak()  # \u0410\u043d\u0430\u043b\u0438\u0437 objgraph.show_most_common_types(limit=5)  # \u041a\u0430\u043a\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043f\u043b\u043e\u0434\u044f\u0442\u0441\u044f? objgraph.show_backrefs([cache], filename=\"graph.png\")  # \u0413\u0440\u0430\u0444 \u0441\u0432\u044f\u0437\u0435\u0439<\/code><\/pre>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0415\u0441\u043b\u0438 \u0432\u0438\u0434\u0438\u0442\u0435 \u0440\u0430\u0441\u0442\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\u00a0dict\u00a0\u0438\u043b\u0438\u00a0list, \u043f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435, \u043d\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0435 \u043b\u0438 \u0432\u044b \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043a\u044d\u0448 \u0431\u0435\u0437 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439.  <\/p>\n<h3>6. \u0420\u0435\u0446\u0435\u043f\u0442\u044b \u0434\u043b\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h3>\n<h4>6.1. \u041a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0435\u0437 \u0443\u0442\u0435\u0447\u0435\u043a: \u0441\u043b\u0430\u0431\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438<\/h4>\n<p>\u041e\u0431\u044b\u0447\u043d\u044b\u0439 \u043a\u044d\u0448 \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0438\u043b\u044c\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438, \u043d\u0435 \u0434\u0430\u0432\u0430\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c \u0443\u0434\u0430\u043b\u044f\u0442\u044c\u0441\u044f.\u00a0<\/p>\n<p><code><strong>WeakValueDictionary<\/strong><\/code>\u00a0\u0440\u0435\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443:<\/p>\n<pre><code class=\"python\">import weakref  class Cache:     def __init__(self):         self._data = weakref.WeakValueDictionary()      def get(self, key):         return self._data.get(key)      def set(self, key, value):         self._data[key] = value  # \u041e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u043a\u044d\u0448\u0435 \u0443\u0434\u0430\u043b\u044f\u044e\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430 \u043d\u0438\u0445 \u043d\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0441\u044b\u043b\u043e\u043a<\/code><\/pre>\n<p><strong>\u0421\u043e\u0432\u0435\u0442:<\/strong>\u00a0\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u0441\u043b\u0430\u0431\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u0434\u043b\u044f \u043a\u044d\u0448\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u043b\u0438\u044f\u0442\u044c \u043d\u0430 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u044d\u0448 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439).<\/p>\n<h4>6.2. \u041f\u0443\u043b\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432:\u00a0tuple\u00a0vs\u00a0list<\/h4>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0442\u0438\u043f\u044b (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440,\u00a0tuple) \u0434\u043b\u044f \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"python\"># \u041f\u043b\u043e\u0445\u043e: points = [ [x, y] for x, y in coordinates ]  # \u041a\u0430\u0436\u0434\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u2014 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442  # \u041b\u0443\u0447\u0448\u0435: points = [ (x, y) for x, y in coordinates ]  # \u041a\u043e\u0440\u0442\u0435\u0436\u0438 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438<\/code><\/pre>\n<h4>6.3. \u041b\u0435\u043d\u0438\u0432\u044b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u00a0functools.lru_cache<\/h4>\n<p>\u041a\u044d\u0448\u0438\u0440\u0443\u0439\u0442\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">from functools import lru_cache  @lru_cache(maxsize=1000)  # \u041d\u0435 \u0431\u043e\u043b\u0435\u0435 1000 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 def calculate(x):     return x ** 2<\/code><\/pre>\n<h3>7. \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435: \u0434\u043e\u0432\u0435\u0440\u044f\u0439, \u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0439<\/h3>\n<p>Python \u2014 \u043a\u0430\u043a \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0435\u0440\u0435\u0442 \u043d\u0430 \u0441\u0435\u0431\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e. \u041d\u043e \u0434\u0430\u0436\u0435 \u043b\u0443\u0447\u0448\u0438\u0435 \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a\u0438 \u0438\u043d\u043e\u0433\u0434\u0430 \u043e\u0448\u0438\u0431\u0430\u044e\u0442\u0441\u044f. \u0412\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043a\u0430\u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430:<\/p>\n<ul>\n<li>\n<p><strong>\u041f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u044b<\/strong>\u00a0\u0440\u0430\u0431\u043e\u0442\u044b \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u0430 \u0441\u0441\u044b\u043b\u043e\u043a \u0438 GC.<\/p>\n<\/li>\n<li>\n<p><strong>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b<\/strong>\u00a0(tracemalloc,\u00a0objgraph) \u0434\u043b\u044f \u043e\u0442\u043b\u0430\u0434\u043a\u0438.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b<\/strong>\u00a0(__slots__, \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b, \u0441\u043b\u0430\u0431\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438) \u0432 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u044b\u0445 \u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u0435\u0441\u0442\u0430\u0445.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u043e\u043c\u043d\u0438\u0442\u0435: \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0440\u0430\u0434\u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u0430. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0438\u0448\u0438\u0442\u0435 \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u0439 \u043a\u043e\u0434, \u0430 \u0437\u0430\u0442\u0435\u043c &#171;\u0442\u044e\u043d\u0438\u0433\u0443\u0439\u0442\u0435&#187; \u0435\u0433\u043e, \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0432\u0438\u0434\u0438\u0442\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b. <\/p>\n<p>\u041a\u0430\u043a \u0433\u043e\u0432\u043e\u0440\u0438\u043b \u0414\u043e\u043d\u0430\u043b\u044c\u0434 \u041a\u043d\u0443\u0442:<\/p>\n<blockquote>\n<p>\u00a0&#171;<em>\u041f\u0440\u0435\u0436\u0434\u0435\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u2014 \u043a\u043e\u0440\u0435\u043d\u044c \u0432\u0441\u0435\u0445 \u0437\u043e\u043b<\/em>&#171;.<\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/892922\/\"> https:\/\/habr.com\/ru\/articles\/892922\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-452521","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/452521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=452521"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/452521\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=452521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=452521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=452521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}