{"id":454106,"date":"2025-04-01T16:17:26","date_gmt":"2025-04-01T16:17:26","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=454106"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=454106","title":{"rendered":"<span>\u041e\u0442\u043b\u0430\u0434\u043a\u0430 \u0441 sys._getframe \u0432 Python<\/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<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0432\u00a0\u043a\u043e\u0440\u043e\u0442\u043a\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0441\u00a0\u0442\u0435\u043c, \u0447\u0442\u043e\u00a0\u0436\u0435 \u0442\u0432\u043e\u0440\u0438\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 CPython, \u043a\u043e\u0433\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430: <code>sys._getframe<\/code>, <code>f_back<\/code>, <code>f_globals<\/code>, <code>f_locals<\/code>, \u0430\u00a0\u0442\u0430\u043a\u00a0\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0432\u043e\u0438 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b.<\/p>\n<h3>\u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e call stack \u0432 CPython<\/h3>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u00a0Python, \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <code><strong>frame<\/strong><\/code>. \u042d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0441\u00a0\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435\u0439 \u0432\u00a0\u0434\u043d\u0435\u0432\u043d\u0438\u043a\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u043d\u0430\u00a0\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u0430 \u0432\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e\u00a0\u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0432\u044b\u0437\u043e\u0432\u0435. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e\u00a0\u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432\u00a0\u043a\u0430\u0436\u0434\u043e\u043c \u0442\u0430\u043a\u043e\u043c \u043a\u0430\u0434\u0440\u0435:<\/p>\n<ul>\n<li>\n<p><strong>\u0418\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0435\u0451 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434:<\/strong><\/p>\n<\/li>\n<li>\n<p><code>f_code<\/code>\u00a0\u2014 \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u0431\u0430\u0439\u0442\u2011\u043a\u043e\u0434, \u0438\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430 \u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. \u0418\u043c\u0435\u043d\u043d\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u043b\u044e \u043c\u043e\u0436\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c, \u043a\u0430\u043a\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u0435\u0439\u0447\u0430\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a\u00a0\u0435\u0451 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u043a\u043e\u0434\u0443, \u0435\u0441\u043b\u0438 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u043e\u043c\u0435\u0440 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 <\/strong><code>f_lineno<\/code><strong>:<\/strong><br \/>\u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u043a\u0430\u043a\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u00a0\u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442. \u0415\u0441\u043b\u0438 \u0432\u044b \u043a\u043e\u0433\u0434\u0430\u2011\u043d\u0438\u0431\u0443\u0434\u044c \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u043b\u0438 \u043a\u043e\u0434 \u0438 \u043f\u044b\u0442\u0430\u043b\u0438\u0441\u044c \u043f\u043e\u043d\u044f\u0442\u044c, \u0433\u0434\u0435 \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u0430 \u043e\u0448\u0438\u0431\u043a\u0430, \u044d\u0442\u043e\u0442 \u043d\u043e\u043c\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u043a\u043b\u044e\u0447\u043e\u043c \u043a\u00a0\u0440\u0430\u0437\u0433\u0430\u0434\u043a\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u043b\u043e\u0432\u0430\u0440\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 <\/strong><code>f_locals<\/code><strong> \u0438 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 <\/strong><code>f_global<\/code><strong>:<\/strong><br \/>\u042d\u0442\u0438 \u0441\u043b\u043e\u0432\u0430\u0440\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u0432\u00a0\u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442. \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435\u00a0\u2014 \u044d\u0442\u043e \u0442\u0435, \u0447\u0442\u043e\u00a0\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430\u00a0\u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435\u00a0\u2014 \u043e\u0431\u0449\u0438\u0435 \u0434\u043b\u044f\u00a0\u0432\u0441\u0435\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f. <\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430\u00a0\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043a\u0430\u0434\u0440 <\/strong><code>f_back<\/code><strong>:<\/strong><br \/>\u042d\u0442\u043e \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430\u00a0\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0432\u044b\u0437\u043e\u0432 \u0432\u00a0\u0441\u0442\u0435\u043a\u0435. \u041a\u0430\u0436\u0434\u044b\u0439 \u043a\u0430\u0434\u0440\u00a0\u2014 \u044d\u0442\u043e \u0431\u0443\u043c\u0430\u0436\u043d\u0430\u044f \u0437\u0430\u043c\u0435\u0442\u043a\u0430, \u0433\u0434\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e: \u00ab\u042f\u00a0\u0431\u044b\u043b \u0432\u044b\u0437\u0432\u0430\u043d \u0438\u0437\u00a0\u044d\u0442\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0437\u0430\u043c\u0435\u0442\u043a\u0438\u00bb. \u0421\u0432\u044f\u0437\u044c \u0447\u0435\u0440\u0435\u0437 <code>f_back<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442, \u0434\u0432\u0438\u0433\u0430\u044f\u0441\u044c \u00ab\u043d\u0430\u0437\u0430\u0434\u00bb, \u0440\u0435\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0435\u0441\u044c \u043f\u0443\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u043e\u0442\u00a0\u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u00a0\u0442\u043e\u0447\u043a\u0438 \u0432\u0445\u043e\u0434\u0430 \u0432\u00a0\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443.<\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a\u00a0\u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043f\u043e\u00a0\u0432\u0441\u0435\u043c \u043a\u0430\u0434\u0440\u0430\u043c \u0432\u044b\u0437\u043e\u0432\u0430:<\/p>\n<pre><code class=\"python\">import sys  def print_call_stack():     frame = sys._getframe()  # \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043a\u0430\u0434\u0440 (\u0444\u0443\u043d\u043a\u0446\u0438\u044f print_call_stack)     stack = []     while frame:         # \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0443: \u0438\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0442\u0435\u043a\u0443\u0449\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0432 \u043a\u043e\u0434\u0435         stack.append(f\"{frame.f_code.co_name} (line {frame.f_lineno})\")         # \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c\u0443 \u043a\u0430\u0434\u0440\u0443         frame = frame.f_back     print(\"Call Stack (\u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043a \u043d\u0430\u0447\u0430\u043b\u0443):\")     # \u0412\u044b\u0432\u043e\u0434\u0438\u043c \u0441\u0442\u0435\u043a \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 (\u043e\u0442 \u0441\u0430\u043c\u043e\u0439 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u0442\u043e\u0447\u043a\u0438 \u0432\u0445\u043e\u0434\u0430 \u0434\u043e \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)     for entry in reversed(stack):         print(\"  -&gt;\", entry)  def foo():     bar()  def bar():     print_call_stack()  foo()<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>sys._getframe()<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0444\u0440\u0435\u0439\u043c, \u0442\u043e \u0435\u0441\u0442\u044c \u043a\u0430\u0434\u0440, \u0432\u00a0\u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>print_call_stack()<\/code>, \u0438 \u0441\u043b\u0443\u0436\u0438\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u043e\u0439 \u0434\u043b\u044f\u00a0\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u0432\u044b\u0437\u043e\u0432\u043e\u0432; \u0437\u0430\u0442\u0435\u043c, \u0432\u00a0\u0446\u0438\u043a\u043b\u0435 <code>while<\/code>, \u043f\u043e\u043a\u0430 \u043a\u0430\u0434\u0440 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u043c\u044b \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c \u0438\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 <code>frame.f_code.co_name<\/code> \u0438 \u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0440\u043e\u043a\u0438 \u0447\u0435\u0440\u0435\u0437 <code>frame.f_lineno<\/code>, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u044d\u0442\u0443 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432\u00a0\u0441\u043f\u0438\u0441\u043e\u043a <code>stack<\/code> \u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a\u00a0\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c\u0443 \u043a\u0430\u0434\u0440\u0443 \u0447\u0435\u0440\u0435\u0437 <code>frame.f_back;<\/code> \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0446\u0438\u043a\u043b\u0430 \u043c\u044b \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0441\u0442\u0435\u043a \u0432\u00a0\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u043f\u043e\u043b\u0443\u0447\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u00a0\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 (\u0442\u043e\u0447\u043a\u0430 \u0432\u0445\u043e\u0434\u0430) \u043a\u00a0\u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443, \u0433\u0434\u0435 \u0441\u0430\u043c\u044b\u0439 \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u00a0\u2014 \u044d\u0442\u043e \u043d\u0430\u0447\u0430\u043b\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0430\u00a0\u043d\u0438\u0436\u043d\u0438\u0439\u00a0\u2014 \u0441\u0430\u043c\u044b\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0432\u044b\u0437\u043e\u0432.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043f\u0440\u0438\u00a0\u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430\u00a0\u0431\u044b\u043b \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<pre><code class=\"bash\">Call Stack (\u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043a \u043d\u0430\u0447\u0430\u043b\u0443):   -&gt; _run_module_as_main (line 198)   -&gt; _run_code (line 88)   -&gt; &lt;module&gt; (line 37)   -&gt; launch_instance (line 992)   -&gt; start (line 712)   -&gt; start (line 205)   -&gt; run_forever (line 608)   -&gt; _run_once (line 1936)   -&gt; _run (line 84)   -&gt; dispatch_queue (line 510)   -&gt; process_one (line 499)   -&gt; dispatch_shell (line 406)   -&gt; execute_request (line 730)   -&gt; do_execute (line 383)   -&gt; run_cell (line 528)   -&gt; run_cell (line 2975)   -&gt; _run_cell (line 3030)   -&gt; _pseudo_sync_runner (line 78)   -&gt; run_cell_async (line 3257)   -&gt; run_ast_nodes (line 3473)   -&gt; run_code (line 3553)   -&gt; &lt;cell line: 0&gt; (line 19)   -&gt; foo (line 14)   -&gt; bar (line 17)   -&gt; print_call_stack (line 7)<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u0430\u044f \u0446\u0435\u043f\u043e\u0447\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441\u00a0\u0441\u0430\u043c\u044b\u0445 \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>runmodule_as_main<\/code>, <code>runcode<\/code>, <code>&lt;module&gt;<\/code>), \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0441\u0440\u0435\u0434\u044b (<code>launch_instance<\/code>, <code>run_forever<\/code> \u0438 \u043f\u0440\u043e\u0447\u0438\u0435, \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u043d\u044b\u0435 \u0434\u043b\u044f\u00a0\u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043e\u0431\u043e\u043b\u043e\u0447\u0435\u043a, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a\u00a0Jupyter\/IPython), \u0434\u043e\u00a0\u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430, \u0433\u0434\u0435 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0\u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>foo<\/code> (line 14) \u0432\u044b\u0437\u0432\u0430\u043b\u0430 <code>bar <\/code>(line 17), \u043a\u043e\u0442\u043e\u0440\u0430\u044f, \u0432\u00a0\u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0432\u044b\u0437\u0432\u0430\u043b\u0430 <code>print_call_stack<\/code> (line 7); \u0442\u0430\u043a\u043e\u0439 \u0432\u044b\u0432\u043e\u0434 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u043a\u0430\u043a\u00a0\u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0443\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430, \u0438 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u043b\u043e\u043a\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c, \u043d\u0430\u00a0\u043a\u0430\u043a\u043e\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0430\u043f\u0435 (\u0438 \u0432\u00a0\u043a\u0430\u043a\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435) \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u0451\u043b \u0432\u044b\u0437\u043e\u0432. <\/p>\n<h3>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0442\u0440\u0435\u0439\u0441\u0435\u0440\u0430: \u043e\u0442\u043b\u0430\u0434\u043a\u0430 \u0431\u0435\u0437 pdb<\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043b\u0435\u0433\u043a\u043e\u0432\u0435\u0441\u043d\u044b\u0439 \u043e\u0442\u043b\u0430\u0434\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u0441\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u0432\u00a0Python \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>sys.settrace<\/code>, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0430\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0441\u043e\u0431\u044b\u0442\u0438\u0439.<\/p>\n<p>\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0442\u0440\u0435\u0439\u0441\u0435\u0440 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u0432:<\/p>\n<pre><code class=\"python\">import sys  def simple_tracer(frame, event, arg):     if event == \"call\":         code = frame.f_code         func_name = code.co_name         line_no = frame.f_lineno         print(f\"[CALL] {func_name} at line {line_no}\")     elif event == \"return\":         code = frame.f_code         func_name = code.co_name         print(f\"[RETURN] {func_name} returning {arg}\")     return simple_tracer  def traced_function(x):     return x * 2  def another_traced_function(y):     result = traced_function(y)     return result + 1  def run_tracer():     sys.settrace(simple_tracer)     print(\"Result:\", another_traced_function(5))     sys.settrace(None)  run_tracer()<\/code><\/pre>\n<p>\u0412\u00a0\u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"bash\">[CALL] another_traced_function at line 18 [CALL] traced_function at line 15 [RETURN] traced_function returning 10 [RETURN] another_traced_function returning 11 [CALL] write at line 526 [CALL] _is_master_process at line 437 [RETURN] _is_master_process returning True Result:[CALL] _schedule_flush at line 456 [RETURN] _schedule_flush returning None [RETURN] write returning 7 [CALL] write at line 526 [CALL] _is_master_process at line 437 [RETURN] _is_master_process returning True  [CALL] _schedule_flush at line 456 [RETURN] _schedule_flush returning None [RETURN] write returning 1 [CALL] write at line 526 [CALL] _is_master_process at line 437 [RETURN] _is_master_process returning True 11[CALL] _schedule_flush at line 456 [RETURN] _schedule_flush returning None [RETURN] write returning 2 [CALL] write at line 526 [CALL] _is_master_process at line 437 [RETURN] _is_master_process returning True  [CALL] _schedule_flush at line 456 [RETURN] _schedule_flush returning None [RETURN] write returning 1<\/code><\/pre>\n<p>\u0422\u0440\u0435\u0439\u0441\u0435\u0440 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u044b \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043d\u0435\u00a0\u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u00a0\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u043d\u043e\u00a0\u0438 \u0438\u0437\u00a0\u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438\u00a0\u043f\u0435\u0447\u0430\u0442\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430. \u0412\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>another_traced_function<\/code> (\u0441\u0442\u0440\u043e\u043a\u0430 18), \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <code>traced_function<\/code> (\u0441\u0442\u0440\u043e\u043a\u0430 15); \u0442\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 10, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e <code>another_traced_function<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 11. \u0417\u0430\u0442\u0435\u043c, \u043a\u043e\u0433\u0434\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 print, \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u044e\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u044b: \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>write<\/code>, <code>ismaster_process<\/code>, <code>scheduleflush <\/code>\u2014 \u043e\u043d\u0438 \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0442 \u0437\u0430\u00a0\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044e \u0432\u044b\u0432\u043e\u0434\u0430 \u0432\u00a0\u043a\u043e\u043d\u0441\u043e\u043b\u044c.<\/p>\n<h3>\u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b \u0441 \u0434\u043e\u0441\u0442\u0443\u043f\u043e\u043c \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 \u0432\u044b\u0437\u043e\u0432\u0430<\/h3>\n<p>\u0427\u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u043d\u0435\u00a0\u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0430\u00a0\u0435\u0449\u0435 \u0438 \u0443\u0437\u043d\u0430\u0442\u044c, \u043a\u0442\u043e \u0435\u0451 \u0432\u044b\u0437\u0432\u0430\u043b? \u0422\u0443\u0442 \u043d\u0430\u00a0\u043f\u043e\u043c\u043e\u0449\u044c \u0441\u043d\u043e\u0432\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 <code>sys._getframe<\/code>.<\/p>\n<pre><code class=\"python\">import sys from functools import wraps  def log_call(func):     @wraps(func)     def wrapper(*args, **kwargs):         # \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043a\u0430\u0434\u0440 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438         caller_frame = sys._getframe(1)         caller_name = caller_frame.f_code.co_name         caller_line = caller_frame.f_lineno         print(f\"[LOG] \u0424\u0443\u043d\u043a\u0446\u0438\u044f '{func.__name__}' \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 '{caller_name}' \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 {caller_line}\")         result = func(*args, **kwargs)         print(f\"[LOG] \u0424\u0443\u043d\u043a\u0446\u0438\u044f '{func.__name__}' \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0430\u0441\u044c \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c {result}\")         return result     return wrapper  @log_call def compute_area(radius):     from math import pi     return pi * radius ** 2  def main():     area = compute_area(5)     print(f\"\u041f\u043b\u043e\u0449\u0430\u0434\u044c \u043a\u0440\u0443\u0433\u0430: {area}\")  main()<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434:<\/p>\n<pre><code class=\"bash\">[LOG] \u0424\u0443\u043d\u043a\u0446\u0438\u044f 'compute_area' \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 'main' \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 23 [LOG] \u0424\u0443\u043d\u043a\u0446\u0438\u044f 'compute_area' \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0430\u0441\u044c \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c 78.53981633974483 \u041f\u043b\u043e\u0449\u0430\u0434\u044c \u043a\u0440\u0443\u0433\u0430: 78.53981633974483<\/code><\/pre>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u0445\u043e\u0447\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0442\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u043a\u043b\u044e\u0447\u0430\u043b\u0430\u0441\u044c \u043d\u0435\u00a0\u0432\u0441\u0435\u0433\u0434\u0430, \u0430\u00a0\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u00a0\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"python\">def conditional_trace(threshold):     def decorator(func):         @wraps(func)         def wrapper(*args, **kwargs):             # \u0415\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u2014 \u0447\u0438\u0441\u043b\u043e \u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0442\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0443             if args and isinstance(args[0], (int, float)) and args[0] &gt; threshold:                 print(f\"[TRACE] {func.__name__} \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0441 args={args} kwargs={kwargs}\")                 caller_frame = sys._getframe(1)                 print(f\"[TRACE] \u0412\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 {caller_frame.f_code.co_name} \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 {caller_frame.f_lineno}\")             result = func(*args, **kwargs)             return result         return wrapper     return decorator  @conditional_trace(10) def multiply(a, b):     return a * b  def test():     print(multiply(5, 3))   # \u0422\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, 5 &lt; 10     print(multiply(15, 2))  # \u0422\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, 15 &gt; 10  test()<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434:<\/p>\n<pre><code class=\"bash\">15 [TRACE] multiply \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0441 args=(15, 2) kwargs={} [TRACE] \u0412\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 test \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 21 30<\/code><\/pre>\n<h3>\u041b\u0435\u0433\u043a\u0438\u0439 \u043f\u0440\u043e\u0444\u0430\u0439\u043b\u0435\u0440 \u043d\u0430 \u0431\u0430\u0437\u0435 sys._getframe<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0435\u00a0\u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u0434, \u0430\u00a0\u0435\u0449\u0435 \u0438 \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, \u0442\u043e \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u043b\u0435\u0433\u043a\u0438\u0439 \u043f\u0440\u043e\u0444\u0430\u0439\u043b\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u043c\u0435\u0440\u044f\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c, \u043e\u0442\u043a\u0443\u0434\u0430 \u043e\u043d\u0438\u00a0\u0431\u044b\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u043d\u044b.<\/p>\n<pre><code class=\"python\">import sys import time from functools import wraps  def profile(func):     @wraps(func)     def wrapper(*args, **kwargs):         start_time = time.perf_counter()         result = func(*args, **kwargs)         end_time = time.perf_counter()         # \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435         caller = sys._getframe(1)         caller_info = f\"{caller.f_code.co_name} (line {caller.f_lineno})\"         print(f\"[PROFILE] \u0424\u0443\u043d\u043a\u0446\u0438\u044f '{func.__name__}' \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 {caller_info} \u0437\u0430\u043d\u044f\u043b\u0430 {end_time - start_time:.6f} \u0441\u0435\u043a\u0443\u043d\u0434\")         return result     return wrapper  @profile def heavy_computation(n):     s = 0     for i in range(n):         s += i ** 2     return s  def run_computation():     result = heavy_computation(100000)     print(\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439:\", result)  run_computation()<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434:<\/p>\n<pre><code class=\"bash\">[PROFILE] \u0424\u0443\u043d\u043a\u0446\u0438\u044f 'heavy_computation' \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 run_computation (line 26) \u0437\u0430\u043d\u044f\u043b\u0430 0.011234 \u0441\u0435\u043a\u0443\u043d\u0434 \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439: 333328333350000<\/code><\/pre>\n<hr\/>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u044d\u0442\u043e \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435\u00a0\u0432\u0441\u0451 \u043f\u0440\u043e\u00a0call stack. \u041f\u043e\u0434\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0441\u0432\u043e\u0438\u043c \u043e\u043f\u044b\u0442\u043e\u043c \u0432\u00a0\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/p>\n<p><em>\u0412\u0441\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u043d\u0430 \u043e\u043d\u043b\u0430\u0439\u043d-\u043a\u0443\u0440\u0441\u0430\u0445 OTUS: <\/em><a href=\"https:\/\/otus.pw\/jzyAg\/\"><em>\u0432 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435<\/em><\/a><em> \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c, \u0430 <\/em><a href=\"https:\/\/otus.pw\/Y3BO\/\"><em>\u0432 \u043a\u0430\u043b\u0435\u043d\u0434\u0430\u0440\u0435<\/em><\/a><em> \u2014 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0435 \u0443\u0440\u043e\u043a\u0438.<\/em><\/p>\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\/893244\/\"> https:\/\/habr.com\/ru\/articles\/893244\/<\/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<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0432\u00a0\u043a\u043e\u0440\u043e\u0442\u043a\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0441\u00a0\u0442\u0435\u043c, \u0447\u0442\u043e\u00a0\u0436\u0435 \u0442\u0432\u043e\u0440\u0438\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 CPython, \u043a\u043e\u0433\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430: <code>sys._getframe<\/code>, <code>f_back<\/code>, <code>f_globals<\/code>, <code>f_locals<\/code>, \u0430\u00a0\u0442\u0430\u043a\u00a0\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0432\u043e\u0438 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b.<\/p>\n<h3>\u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e call stack \u0432 CPython<\/h3>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0432\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u00a0Python, \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 <code><strong>frame<\/strong><\/code>. \u042d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0441\u00a0\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435\u0439 \u0432\u00a0\u0434\u043d\u0435\u0432\u043d\u0438\u043a\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u043d\u0430\u00a0\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u0430 \u0432\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e\u00a0\u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0432\u044b\u0437\u043e\u0432\u0435. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e\u00a0\u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432\u00a0\u043a\u0430\u0436\u0434\u043e\u043c \u0442\u0430\u043a\u043e\u043c \u043a\u0430\u0434\u0440\u0435:<\/p>\n<ul>\n<li>\n<p><strong>\u0418\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0435\u0451 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434:<\/strong><\/p>\n<\/li>\n<li>\n<p><code>f_code<\/code>\u00a0\u2014 \u044d\u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u0431\u0430\u0439\u0442\u2011\u043a\u043e\u0434, \u0438\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430 \u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. \u0418\u043c\u0435\u043d\u043d\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u043b\u044e \u043c\u043e\u0436\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c, \u043a\u0430\u043a\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u0435\u0439\u0447\u0430\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a\u00a0\u0435\u0451 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u043a\u043e\u0434\u0443, \u0435\u0441\u043b\u0438 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u043e\u043c\u0435\u0440 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 <\/strong><code>f_lineno<\/code><strong>:<\/strong><br \/>\u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u043a\u0430\u043a\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u00a0\u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442. \u0415\u0441\u043b\u0438 \u0432\u044b \u043a\u043e\u0433\u0434\u0430\u2011\u043d\u0438\u0431\u0443\u0434\u044c \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u043b\u0438 \u043a\u043e\u0434 \u0438 \u043f\u044b\u0442\u0430\u043b\u0438\u0441\u044c \u043f\u043e\u043d\u044f\u0442\u044c, \u0433\u0434\u0435 \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u0430 \u043e\u0448\u0438\u0431\u043a\u0430, \u044d\u0442\u043e\u0442 \u043d\u043e\u043c\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u043a\u043b\u044e\u0447\u043e\u043c \u043a\u00a0\u0440\u0430\u0437\u0433\u0430\u0434\u043a\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u043b\u043e\u0432\u0430\u0440\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 <\/strong><code>f_locals<\/code><strong> \u0438 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 <\/strong><code>f_global<\/code><strong>:<\/strong><br \/>\u042d\u0442\u0438 \u0441\u043b\u043e\u0432\u0430\u0440\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u0432\u00a0\u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442. \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435\u00a0\u2014 \u044d\u0442\u043e \u0442\u0435, \u0447\u0442\u043e\u00a0\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430\u00a0\u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435\u00a0\u2014 \u043e\u0431\u0449\u0438\u0435 \u0434\u043b\u044f\u00a0\u0432\u0441\u0435\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f. <\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430\u00a0\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043a\u0430\u0434\u0440 <\/strong><code>f_back<\/code><strong>:<\/strong><br \/>\u042d\u0442\u043e \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430\u00a0\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0432\u044b\u0437\u043e\u0432 \u0432\u00a0\u0441\u0442\u0435\u043a\u0435. \u041a\u0430\u0436\u0434\u044b\u0439 \u043a\u0430\u0434\u0440\u00a0\u2014 \u044d\u0442\u043e \u0431\u0443\u043c\u0430\u0436\u043d\u0430\u044f \u0437\u0430\u043c\u0435\u0442\u043a\u0430, \u0433\u0434\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e: \u00ab\u042f\u00a0\u0431\u044b\u043b \u0432\u044b\u0437\u0432\u0430\u043d \u0438\u0437\u00a0\u044d\u0442\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0437\u0430\u043c\u0435\u0442\u043a\u0438\u00bb. \u0421\u0432\u044f\u0437\u044c \u0447\u0435\u0440\u0435\u0437 <code>f_back<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442, \u0434\u0432\u0438\u0433\u0430\u044f\u0441\u044c \u00ab\u043d\u0430\u0437\u0430\u0434\u00bb, \u0440\u0435\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0435\u0441\u044c \u043f\u0443\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u043e\u0442\u00a0\u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u00a0\u0442\u043e\u0447\u043a\u0438 \u0432\u0445\u043e\u0434\u0430 \u0432\u00a0\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443.<\/p>\n<\/li>\n<\/ul>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a\u00a0\u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0439\u0442\u0438\u0441\u044c \u043f\u043e\u00a0\u0432\u0441\u0435\u043c \u043a\u0430\u0434\u0440\u0430\u043c \u0432\u044b\u0437\u043e\u0432\u0430:<\/p>\n<pre><code class=\"python\">import sys  def print_call_stack():     frame = sys._getframe()  # \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043a\u0430\u0434\u0440 (\u0444\u0443\u043d\u043a\u0446\u0438\u044f print_call_stack)     stack = []     while frame:         # \u0424\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0443: \u0438\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0442\u0435\u043a\u0443\u0449\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0432 \u043a\u043e\u0434\u0435         stack.append(f\"{frame.f_code.co_name} (line {frame.f_lineno})\")         # \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c\u0443 \u043a\u0430\u0434\u0440\u0443         frame = frame.f_back     print(\"Call Stack (\u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043a \u043d\u0430\u0447\u0430\u043b\u0443):\")     # \u0412\u044b\u0432\u043e\u0434\u0438\u043c \u0441\u0442\u0435\u043a \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 (\u043e\u0442 \u0441\u0430\u043c\u043e\u0439 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u0442\u043e\u0447\u043a\u0438 \u0432\u0445\u043e\u0434\u0430 \u0434\u043e \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)     for entry in reversed(stack):         print(\"  -&gt;\", entry)  def foo():     bar()  def bar():     print_call_stack()  foo()<\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>sys._getframe()<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0444\u0440\u0435\u0439\u043c, \u0442\u043e \u0435\u0441\u0442\u044c \u043a\u0430\u0434\u0440, \u0432\u00a0\u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>print_call_stack()<\/code>, \u0438 \u0441\u043b\u0443\u0436\u0438\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u043e\u0439 \u0434\u043b\u044f\u00a0\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u0432\u044b\u0437\u043e\u0432\u043e\u0432; \u0437\u0430\u0442\u0435\u043c, \u0432\u00a0\u0446\u0438\u043a\u043b\u0435 <code>while<\/code>, \u043f\u043e\u043a\u0430 \u043a\u0430\u0434\u0440 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u043c\u044b \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c \u0438\u043c\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 <code>frame.f_code.co_name<\/code> \u0438 \u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0440\u043e\u043a\u0438 \u0447\u0435\u0440\u0435\u0437 <code>frame.f_lineno<\/code>, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u044d\u0442\u0443 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432\u00a0\u0441\u043f\u0438\u0441\u043e\u043a <code>stack<\/code> \u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a\u00a0\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c\u0443 \u043a\u0430\u0434\u0440\u0443 \u0447\u0435\u0440\u0435\u0437 <code>frame.f_back;<\/code> \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0446\u0438\u043a\u043b\u0430 \u043c\u044b \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0441\u0442\u0435\u043a \u0432\u00a0\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u043f\u043e\u043b\u0443\u0447\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u00a0\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 (\u0442\u043e\u0447\u043a\u0430 \u0432\u0445\u043e\u0434\u0430) \u043a\u00a0\u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443, \u0433\u0434\u0435 \u0441\u0430\u043c\u044b\u0439 \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u00a0\u2014 \u044d\u0442\u043e \u043d\u0430\u0447\u0430\u043b\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0430\u00a0\u043d\u0438\u0436\u043d\u0438\u0439\u00a0\u2014 \u0441\u0430\u043c\u044b\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0432\u044b\u0437\u043e\u0432.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043f\u0440\u0438\u00a0\u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u0441\u043a\u0440\u0438\u043f\u0442\u0430\u00a0\u0431\u044b\u043b \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<pre><code class=\"bash\">Call Stack (\u043e\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043a \u043d\u0430\u0447\u0430\u043b\u0443):   -&gt; _run_module_as_main (line 198)   -&gt; _run_code (line 88)   -&gt; &lt;module&gt; (line 37)   -&gt; launch_instance (line 992)   -&gt; start (line 712)   -&gt; start (line 205)   -&gt; run_forever (line 608)   -&gt; _run_once (line 1936)   -&gt; _run (line 84)   -&gt; dispatch_queue (line 510)   -&gt; process_one (line 499)   -&gt; dispatch_shell (line 406)   -&gt; execute_request (line 730)   -&gt; do_execute (line 383)   -&gt; run_cell (line 528)   -&gt; run_cell (line 2975)   -&gt; _run_cell (line 3030)   -&gt; _pseudo_sync_runner (line 78)   -&gt; run_cell_async (line 3257)   -&gt; run_ast_nodes (line 3473)   -&gt; run_code (line 3553)   -&gt; &lt;cell line: 0&gt; (line 19)   -&gt; foo (line 14)   -&gt; bar (line 17)   -&gt; print_call_stack (line 7)<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u0430\u044f \u0446\u0435\u043f\u043e\u0447\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441\u00a0\u0441\u0430\u043c\u044b\u0445 \u043d\u0438\u0437\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>runmodule_as_main<\/code>, <code>runcode<\/code>, <code>&lt;module&gt;<\/code>), \u0447\u0435\u0440\u0435\u0437 \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0441\u0440\u0435\u0434\u044b (<code>launch_instance<\/code>, <code>run_forever<\/code> \u0438 \u043f\u0440\u043e\u0447\u0438\u0435, \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u043d\u044b\u0435 \u0434\u043b\u044f\u00a0\u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u043e\u0431\u043e\u043b\u043e\u0447\u0435\u043a, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a\u00a0Jupyter\/IPython), \u0434\u043e\u00a0\u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430, \u0433\u0434\u0435 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0\u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>foo<\/code> (line 14) \u0432\u044b\u0437\u0432\u0430\u043b\u0430 <code>bar <\/code>(line 17), \u043a\u043e\u0442\u043e\u0440\u0430\u044f, \u0432\u00a0\u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0432\u044b\u0437\u0432\u0430\u043b\u0430 <code>print_call_stack<\/code> (line 7); \u0442\u0430\u043a\u043e\u0439 \u0432\u044b\u0432\u043e\u0434 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u043a\u0430\u043a\u00a0\u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0443\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430, \u0438 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u043b\u043e\u043a\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c, \u043d\u0430\u00a0\u043a\u0430\u043a\u043e\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0430\u043f\u0435 (\u0438 \u0432\u00a0\u043a\u0430\u043a\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435) \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u0451\u043b \u0432\u044b\u0437\u043e\u0432. <\/p>\n<h3>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0442\u0440\u0435\u0439\u0441\u0435\u0440\u0430: \u043e\u0442\u043b\u0430\u0434\u043a\u0430 \u0431\u0435\u0437 pdb<\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043b\u0435\u0433\u043a\u043e\u0432\u0435\u0441\u043d\u044b\u0439 \u043e\u0442\u043b\u0430\u0434\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u0441\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439. \u0414\u043b\u044f\u00a0\u044d\u0442\u043e\u0433\u043e \u0432\u00a0Python \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>sys.settrace<\/code>, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0430\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0441\u043e\u0431\u044b\u0442\u0438\u0439.<\/p>\n<p>\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0442\u0440\u0435\u0439\u0441\u0435\u0440 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u0432:<\/p>\n<pre><code class=\"python\">import sys  def simple_tracer(frame, event, arg):     if event == \"call\":         code = frame.f_code         func_name = code.co_name         line_no = frame.f_lineno         print(f\"[CALL] {func_name} at line {line_no}\")     elif event == \"return\":         code = frame.f_code         func_name = code.co_name         print(f\"[RETURN] {func_name} returning {arg}\")     return simple_tracer  def traced_function(x):     return x * 2  def another_traced_function(y):     result = traced_function(y)     return result + 1  def run_tracer():     sys.settrace(simple_tracer)     print(\"Result:\", another_traced_function(5))     sys.settrace(None)  run_tracer()<\/code><\/pre>\n<p>\u0412\u00a0\u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"bash\">[CALL] another_traced_function at line 18 [CALL] traced_function at line 15 [RETURN] traced_function returning 10 [RETURN] another_traced_function returning 11 [CALL] write at line 526 [CALL] _is_master_process at line 437 [RETURN] _is_master_process returning True Result:[CALL] _schedule_flush at line 456 [RETURN] _schedule_flush returning None [RETURN] write returning 7 [CALL] write at line 526 [CALL] _is_master_process at line 437 [RETURN] _is_master_process returning True  [CALL] _schedule_flush at line 456 [RETURN] _schedule_flush returning None [RETURN] write returning 1 [CALL] write at line 526 [CALL] _is_master_process at line 437 [RETURN] _is_master_process returning True 11[CALL] _schedule_flush at line 456 [RETURN] _schedule_flush returning None [RETURN] write returning 2 [CALL] write at line 526 [CALL] _is_master_process at line 437 [RETURN] _is_master_process returning True  [CALL] _schedule_flush at line 456 [RETURN] _schedule_flush returning None [RETURN] write returning 1<\/code><\/pre>\n<p>\u0422\u0440\u0435\u0439\u0441\u0435\u0440 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u044b \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043d\u0435\u00a0\u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u00a0\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u043d\u043e\u00a0\u0438 \u0438\u0437\u00a0\u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438\u00a0\u043f\u0435\u0447\u0430\u0442\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430. \u0412\u0438\u0434\u043d\u043e, \u0447\u0442\u043e\u00a0\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>another_traced_function<\/code> (\u0441\u0442\u0440\u043e\u043a\u0430 18), \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <code>traced_function<\/code> (\u0441\u0442\u0440\u043e\u043a\u0430 15); \u0442\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 10, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e <code>another_traced_function<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 11. \u0417\u0430\u0442\u0435\u043c, \u043a\u043e\u0433\u0434\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 print, \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u044e\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u044b: \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>write<\/code>, <code>ismaster_process<\/code>, <code>scheduleflush <\/code>\u2014 \u043e\u043d\u0438 \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0442 \u0437\u0430\u00a0\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u044e \u0432\u044b\u0432\u043e\u0434\u0430 \u0432\u00a0\u043a\u043e\u043d\u0441\u043e\u043b\u044c.<\/p>\n<h3>\u0414\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u044b \u0441 \u0434\u043e\u0441\u0442\u0443\u043f\u043e\u043c \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443 \u0432\u044b\u0437\u043e\u0432\u0430<\/h3>\n<p>\u0427\u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u043d\u0435\u00a0\u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0430\u00a0\u0435\u0449\u0435 \u0438 \u0443\u0437\u043d\u0430\u0442\u044c, \u043a\u0442\u043e \u0435\u0451 \u0432\u044b\u0437\u0432\u0430\u043b? \u0422\u0443\u0442 \u043d\u0430\u00a0\u043f\u043e\u043c\u043e\u0449\u044c \u0441\u043d\u043e\u0432\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 <code>sys._getframe<\/code>.<\/p>\n<pre><code class=\"python\">import sys from functools import wraps  def log_call(func):     @wraps(func)     def wrapper(*args, **kwargs):         # \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043a\u0430\u0434\u0440 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438         caller_frame = sys._getframe(1)         caller_name = caller_frame.f_code.co_name         caller_line = caller_frame.f_lineno         print(f\"[LOG] \u0424\u0443\u043d\u043a\u0446\u0438\u044f '{func.__name__}' \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 '{caller_name}' \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 {caller_line}\")         result = func(*args, **kwargs)         print(f\"[LOG] \u0424\u0443\u043d\u043a\u0446\u0438\u044f '{func.__name__}' \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0430\u0441\u044c \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c {result}\")         return result     return wrapper  @log_call def compute_area(radius):     from math import pi     return pi * radius ** 2  def main():     area = compute_area(5)     print(f\"\u041f\u043b\u043e\u0449\u0430\u0434\u044c \u043a\u0440\u0443\u0433\u0430: {area}\")  main()<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434:<\/p>\n<pre><code class=\"bash\">[LOG] \u0424\u0443\u043d\u043a\u0446\u0438\u044f 'compute_area' \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 'main' \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 23 [LOG] \u0424\u0443\u043d\u043a\u0446\u0438\u044f 'compute_area' \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0430\u0441\u044c \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c 78.53981633974483 \u041f\u043b\u043e\u0449\u0430\u0434\u044c \u043a\u0440\u0443\u0433\u0430: 78.53981633974483<\/code><\/pre>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u0445\u043e\u0447\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0442\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u043a\u043b\u044e\u0447\u0430\u043b\u0430\u0441\u044c \u043d\u0435\u00a0\u0432\u0441\u0435\u0433\u0434\u0430, \u0430\u00a0\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u00a0\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"python\">def conditional_trace(threshold):     def decorator(func):         @wraps(func)         def wrapper(*args, **kwargs):             # \u0415\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u2014 \u0447\u0438\u0441\u043b\u043e \u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0442\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0443             if args and isinstance(args[0], (int, float)) and args[0] &gt; threshold:                 print(f\"[TRACE] {func.__name__} \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0441 args={args} kwargs={kwargs}\")                 caller_frame = sys._getframe(1)                 print(f\"[TRACE] \u0412\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 {caller_frame.f_code.co_name} \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 {caller_frame.f_lineno}\")             result = func(*args, **kwargs)             return result         return wrapper     return decorator  @conditional_trace(10) def multiply(a, b):     return a * b  def test():     print(multiply(5, 3))   # \u0422\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, 5 &lt; 10     print(multiply(15, 2))  # \u0422\u0440\u0430\u0441\u0441\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, 15 &gt; 10  test()<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434:<\/p>\n<pre><code class=\"bash\">15 [TRACE] multiply \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0441 args=(15, 2) kwargs={} [TRACE] \u0412\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 test \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 21 30<\/code><\/pre>\n<h3>\u041b\u0435\u0433\u043a\u0438\u0439 \u043f\u0440\u043e\u0444\u0430\u0439\u043b\u0435\u0440 \u043d\u0430 \u0431\u0430\u0437\u0435 sys._getframe<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0435\u00a0\u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u0434, \u0430\u00a0\u0435\u0449\u0435 \u0438 \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, \u0442\u043e \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u043b\u0435\u0433\u043a\u0438\u0439 \u043f\u0440\u043e\u0444\u0430\u0439\u043b\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u043c\u0435\u0440\u044f\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c, \u043e\u0442\u043a\u0443\u0434\u0430 \u043e\u043d\u0438\u00a0\u0431\u044b\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u043d\u044b.<\/p>\n<pre><code class=\"python\">import sys import time from functools import wraps  def profile(func):     @wraps(func)     def wrapper(*args, **kwargs):         start_time = time.perf_counter()         result = func(*args, **kwargs)         end_time = time.perf_counter()         # \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435         caller = sys._getframe(1)         caller_info = f\"{caller.f_code.co_name} (line {caller.f_lineno})\"         print(f\"[PROFILE] \u0424\u0443\u043d\u043a\u0446\u0438\u044f '{func.__name__}' \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 {caller_info} \u0437\u0430\u043d\u044f\u043b\u0430 {end_time - start_time:.6f} \u0441\u0435\u043a\u0443\u043d\u0434\")         return result     return wrapper  @profile def heavy_computation(n):     s = 0     for i in range(n):         s += i ** 2     return s  def run_computation():     result = heavy_computation(100000)     print(\"\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439:\", result)  run_computation()<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434:<\/p>\n<pre><code class=\"bash\">[PROFILE] \u0424\u0443\u043d\u043a\u0446\u0438\u044f 'heavy_computation' \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0438\u0437 run_computation (line 26) \u0437\u0430\u043d\u044f\u043b\u0430 0.011234 \u0441\u0435\u043a\u0443\u043d\u0434 \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439: 333328333350000<\/code><\/pre>\n<hr\/>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e,<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-454106","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/454106","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=454106"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/454106\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=454106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=454106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=454106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}