{"id":939659,"date":"2024-12-26T20:40:07","date_gmt":"2024-12-26T12:40:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/939659.html"},"modified":"2024-12-26T20:40:10","modified_gmt":"2024-12-26T12:40:10","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%92%a4%e5%9b%9e","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/939659.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u64a4\u56de"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074627\/afda79c1-88d0-4b1f-bf80-0d25e8c2aa8a.webp\" alt=\"python\u4e2d\u5982\u4f55\u64a4\u56de\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u201c\u64a4\u56de\u201d\u8fd9\u4e00\u6982\u5ff5\u53ef\u80fd\u6307\u7684\u662f\u64a4\u9500\u67d0\u4e9b\u64cd\u4f5c\u6216\u6062\u590d\u4e4b\u524d\u7684\u72b6\u6001\u3002\u5bf9\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u548c\u9700\u6c42\uff0c\u5904\u7406\u201c\u64a4\u56de\u201d\u7684\u65b9\u6cd5\u4f1a\u6709\u6240\u4e0d\u540c\u3002<strong>\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u7248\u672c\u63a7\u5236\u3001\u4e8b\u52a1\u5904\u7406\u3001\u4fdd\u5b58\u72b6\u6001\u5feb\u7167\u3001\u4f7f\u7528\u64a4\u9500\u6808\u7b49\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528\u64a4\u9500\u6808\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u64a4\u56de\u5728Python\u4e2d\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u64a4\u56de\u529f\u80fd\u901a\u5e38\u5e94\u7528\u4e8e\u4ee5\u4e0b\u51e0\u4e2a\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6587\u672c\u7f16\u8f91\u5668\u6216\u7ed8\u56fe\u5e94\u7528\u4e2d\u7684\u64a4\u9500\u64cd\u4f5c<\/strong>\uff1a\u7528\u6237\u53ef\u80fd\u5e0c\u671b\u64a4\u9500\u6700\u8fd1\u7684\u66f4\u6539\u3002<\/li>\n<li><strong>\u6570\u636e\u5e93\u4e8b\u52a1<\/strong>\uff1a\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c\u5982\u679c\u67d0\u4e00\u6b65\u5931\u8d25\uff0c\u9700\u8981\u56de\u6eda\u5230\u4e4b\u524d\u7684\u72b6\u6001\u3002<\/li>\n<li><strong>\u6e38\u620f\u5f00\u53d1<\/strong>\uff1a\u5141\u8bb8\u73a9\u5bb6\u64a4\u9500\u67d0\u4e00\u6b65\u64cd\u4f5c\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u6e38\u620f\u72b6\u6001\u3002<\/li>\n<li><strong>\u6570\u636e\u5904\u7406\u4e0e\u5206\u6790<\/strong>\uff1a\u5728\u6570\u636e\u6e05\u6d17\u6216\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u67d0\u4e00\u6b65\u64cd\u4f5c\u9519\u8bef\uff0c\u53ef\u4ee5\u64a4\u9500\u5230\u4e4b\u524d\u7684\u6b65\u9aa4\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u64a4\u56de\u7684\u57fa\u672c\u5b9e\u73b0\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u64a4\u9500\u6808<\/h4>\n<\/p>\n<p><p>\u64a4\u9500\u6808\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5b9e\u73b0\u64a4\u56de\u529f\u80fd\u7684\u65b9\u6cd5\u3002\u5728\u8fdb\u884c\u6bcf\u4e00\u6b65\u64cd\u4f5c\u65f6\uff0c\u5c06\u5f53\u524d\u72b6\u6001\u538b\u5165\u6808\u4e2d\u3002\u5f53\u9700\u8981\u64a4\u9500\u65f6\uff0c\u4ece\u6808\u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u72b6\u6001\u5e76\u6062\u590d\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u5b9e\u73b0<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">class UndoStack:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.stack = []<\/p>\n<p>        self.current_state = None<\/p>\n<p>    def do(self, action, new_state):<\/p>\n<p>        # \u5c06\u5f53\u524d\u72b6\u6001\u63a8\u5165\u6808\u4e2d<\/p>\n<p>        self.stack.append(self.current_state)<\/p>\n<p>        # \u6267\u884c\u52a8\u4f5c\u5e76\u66f4\u65b0\u72b6\u6001<\/p>\n<p>        self.current_state = action(new_state)<\/p>\n<p>    def undo(self):<\/p>\n<p>        if self.stack:<\/p>\n<p>            # \u6062\u590d\u5230\u4e0a\u4e00\u4e2a\u72b6\u6001<\/p>\n<p>            self.current_state = self.stack.pop()<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;No more actions to undo.&quot;)<\/p>\n<h2><strong>\u793a\u4f8b\u52a8\u4f5c\u51fd\u6570<\/strong><\/h2>\n<p>def example_action(new_state):<\/p>\n<p>    return new_state<\/p>\n<h2><strong>\u4f7f\u7528\u64a4\u9500\u6808<\/strong><\/h2>\n<p>undo_stack = UndoStack()<\/p>\n<p>undo_stack.do(example_action, &quot;State 1&quot;)<\/p>\n<p>undo_stack.do(example_action, &quot;State 2&quot;)<\/p>\n<p>print(&quot;Current state:&quot;, undo_stack.current_state)<\/p>\n<p>undo_stack.undo()<\/p>\n<p>print(&quot;After undo:&quot;, undo_stack.current_state)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6570\u636e\u5e93\u4e8b\u52a1<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u4e8b\u52a1\u662f\u4e00\u7ec4\u64cd\u4f5c\u7684\u96c6\u5408\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u8981\u4e48\u5168\u90e8\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u5931\u8d25\u3002\u5728\u4e8b\u52a1\u4e2d\u4f7f\u7528\u201c\u64a4\u56de\u201d\u662f\u901a\u8fc7\u56de\u6eda\uff08rollback\uff09\u5b9e\u73b0\u7684\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u5b9e\u73b0<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u8fde\u63a5<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;:memory:&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>cursor.execute(&#39;CREATE TABLE example (id INTEGER PRIMARY KEY, value TEXT)&#39;)<\/p>\n<h2><strong>\u5f00\u59cb\u4e8b\u52a1<\/strong><\/h2>\n<p>conn.execute(&#39;BEGIN&#39;)<\/p>\n<p>try:<\/p>\n<p>    # \u63d2\u5165\u6570\u636e<\/p>\n<p>    cursor.execute(&#39;INSERT INTO example (value) VALUES (?)&#39;, (&#39;Data 1&#39;,))<\/p>\n<p>    cursor.execute(&#39;INSERT INTO example (value) VALUES (?)&#39;, (&#39;Data 2&#39;,))<\/p>\n<p>    # \u63d0\u4ea4\u4e8b\u52a1<\/p>\n<p>    conn.commit()<\/p>\n<p>except Exception as e:<\/p>\n<p>    # \u53d1\u751f\u9519\u8bef\u65f6\u56de\u6eda<\/p>\n<p>    conn.rollback()<\/p>\n<p>    print(&quot;Error occurred, transaction rolled back.&quot;, e)<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM example&#39;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>print(&quot;Data in table:&quot;, rows)<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u64a4\u56de\u529f\u80fd\u7684\u9ad8\u7ea7\u5b9e\u73b0<\/h3>\n<\/p>\n<p><h4>1. \u72b6\u6001\u5feb\u7167<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u5e94\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528\u72b6\u6001\u5feb\u7167\u6765\u4fdd\u5b58\u548c\u6062\u590d\u72b6\u6001\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u6df1\u5ea6\u590d\u5236\u5bf9\u8c61\u7684\u72b6\u6001\uff0c\u4ee5\u786e\u4fdd\u53ef\u4ee5\u5b8c\u5168\u6062\u590d\u3002<\/p>\n<\/p>\n<p><h5>\u793a\u4f8b\u5b9e\u73b0<\/h5>\n<\/p>\n<p><pre><code class=\"language-python\">import copy<\/p>\n<p>class SnapshotManager:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.snapshots = []<\/p>\n<p>        self.current_state = None<\/p>\n<p>    def save_snapshot(self):<\/p>\n<p>        # \u6df1\u5ea6\u590d\u5236\u5f53\u524d\u72b6\u6001<\/p>\n<p>        self.snapshots.append(copy.deepcopy(self.current_state))<\/p>\n<p>    def restore_snapshot(self):<\/p>\n<p>        if self.snapshots:<\/p>\n<p>            # \u6062\u590d\u5230\u6700\u8fd1\u7684\u5feb\u7167<\/p>\n<p>            self.current_state = self.snapshots.pop()<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;No snapshots to restore.&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5feb\u7167\u7ba1\u7406\u5668<\/strong><\/h2>\n<p>snapshot_manager = SnapshotManager()<\/p>\n<p>snapshot_manager.current_state = {&quot;key&quot;: &quot;value1&quot;}<\/p>\n<p>snapshot_manager.save_snapshot()<\/p>\n<p>snapshot_manager.current_state[&quot;key&quot;] = &quot;value2&quot;<\/p>\n<p>print(&quot;Current state:&quot;, snapshot_manager.current_state)<\/p>\n<p>snapshot_manager.restore_snapshot()<\/p>\n<p>print(&quot;After restoring:&quot;, snapshot_manager.current_state)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u64a4\u56de\u529f\u80fd\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u9891\u7e41\u4fdd\u5b58\u548c\u6062\u590d\u72b6\u6001\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u65f6\u3002<\/li>\n<li><strong>\u5b58\u50a8\u7a7a\u95f4<\/strong>\uff1a\u4fdd\u5b58\u72b6\u6001\u5feb\u7167\u6216\u4f7f\u7528\u64a4\u9500\u6808\u9700\u8981\u989d\u5916\u7684\u5b58\u50a8\u7a7a\u95f4\u3002<\/li>\n<li><strong>\u590d\u6742\u6027<\/strong>\uff1a\u5b9e\u73b0\u64a4\u56de\u529f\u80fd\u53ef\u80fd\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u590d\u6742\u6027\uff0c\u9700\u8981\u4ed4\u7ec6\u8bbe\u8ba1\u548c\u6d4b\u8bd5\u3002<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9e\u73b0\u64a4\u56de\u529f\u80fd\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u4f7f\u7528\u64a4\u9500\u6808\u3001\u6570\u636e\u5e93\u4e8b\u52a1\u3001\u72b6\u6001\u5feb\u7167<\/strong>\u7b49\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u6743\u8861\u548c\u9009\u62e9\u3002\u901a\u8fc7\u5408\u7406\u7684\u8bbe\u8ba1\u548c\u5b9e\u73b0\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u4f9b\u64a4\u56de\u529f\u80fd\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u548c\u5e94\u7528\u7684\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u5b9e\u73b0\u64a4\u56de\u64cd\u4f5c\uff1f<\/strong><br \/>Python\u5e76\u6ca1\u6709\u5185\u7f6e\u7684\u64a4\u56de\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u7ed3\u6784\u548c\u903b\u8f91\u5b9e\u73b0\u7c7b\u4f3c\u6548\u679c\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5217\u8868\u6216\u6808\u6765\u5b58\u50a8\u64cd\u4f5c\u5386\u53f2\u8bb0\u5f55\u3002\u5f53\u7528\u6237\u9700\u8981\u64a4\u56de\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u4ece\u5386\u53f2\u8bb0\u5f55\u4e2d\u5f39\u51fa\u6700\u540e\u7684\u64cd\u4f5c\uff0c\u5e76\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\u3002\u4f60\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u51fd\u6570\u548c\u7c7b\u6765\u5c01\u88c5\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u4ee5\u4fbf\u4e8e\u7ba1\u7406\u548c\u8c03\u7528\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5b9e\u73b0\u64a4\u56de\u529f\u80fd\u9700\u8981\u54ea\u4e9b\u57fa\u672c\u77e5\u8bc6\uff1f<\/strong><br \/>\u5b9e\u73b0\u64a4\u56de\u529f\u80fd\u901a\u5e38\u6d89\u53ca\u5230\u5bf9\u6570\u636e\u7ed3\u6784\u7684\u7406\u89e3\uff0c\u7279\u522b\u662f\u5217\u8868\u548c\u6808\u7684\u64cd\u4f5c\u3002\u540c\u65f6\uff0c\u4f60\u9700\u8981\u638c\u63e1Python\u7684\u51fd\u6570\u5b9a\u4e49\u3001\u5f02\u5e38\u5904\u7406\u4ee5\u53ca\u5bf9\u8c61\u5bfc\u5411\u7f16\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\u3002\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Python\u7684\u5e93\uff08\u5982deque\uff09\u6765\u9ad8\u6548\u7ba1\u7406\u64cd\u4f5c\u5386\u53f2\u4e5f\u662f\u6709\u5e2e\u52a9\u7684\u3002<\/p>\n<p><strong>\u64a4\u56de\u64cd\u4f5c\u5728Python\u5e94\u7528\u4e2d\u6709\u54ea\u4e9b\u5b9e\u9645\u573a\u666f\uff1f<\/strong><br \/>\u64a4\u56de\u64cd\u4f5c\u5728\u5f88\u591a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u90fd\u5f88\u5e38\u89c1\uff0c\u7279\u522b\u662f\u5728\u6587\u672c\u7f16\u8f91\u5668\u3001\u56fe\u5f62\u7f16\u8f91\u8f6f\u4ef6\u548c\u6e38\u620f\u4e2d\u3002\u4f8b\u5982\uff0c\u6587\u672c\u7f16\u8f91\u5668\u901a\u5e38\u5141\u8bb8\u7528\u6237\u64a4\u56de\u8f93\u5165\u7684\u66f4\u6539\uff0c\u800c\u56fe\u5f62\u8f6f\u4ef6\u5219\u53ef\u80fd\u5141\u8bb8\u7528\u6237\u64a4\u56de\u6700\u8fd1\u7684\u7ed8\u56fe\u64cd\u4f5c\u3002\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0c\u64a4\u56de\u529f\u80fd\u53ef\u4ee5\u5e2e\u52a9\u73a9\u5bb6\u6062\u590d\u5230\u67d0\u4e2a\u7279\u5b9a\u7684\u6e38\u620f\u72b6\u6001\uff0c\u589e\u52a0\u7528\u6237\u4f53\u9a8c\u548c\u64cd\u4f5c\u7684\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u201c\u64a4\u56de\u201d\u8fd9\u4e00\u6982\u5ff5\u53ef\u80fd\u6307\u7684\u662f\u64a4\u9500\u67d0\u4e9b\u64cd\u4f5c\u6216\u6062\u590d\u4e4b\u524d\u7684\u72b6\u6001\u3002\u5bf9\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u548c\u9700\u6c42\uff0c\u5904\u7406\u201c\u64a4\u56de\u201d\u7684 [&hellip;]","protected":false},"author":3,"featured_media":939663,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/939659"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=939659"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/939659\/revisions"}],"predecessor-version":[{"id":939664,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/939659\/revisions\/939664"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/939663"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=939659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=939659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=939659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}