{"id":1123418,"date":"2025-01-08T19:32:07","date_gmt":"2025-01-08T11:32:07","guid":{"rendered":""},"modified":"2025-01-08T19:32:11","modified_gmt":"2025-01-08T11:32:11","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e4%b8%8a%e4%b8%80%e6%ad%a5%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1123418.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084859\/ab428305-e501-4f29-9dbb-ddd55ee3b63e.webp\" alt=\"python\u4e2d\u5982\u4f55\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u64a4\u9500\u64cd\u4f5c\u3001\u72b6\u6001\u4fdd\u5b58\u3001\u4ee5\u53ca\u5229\u7528\u6570\u636e\u7ed3\u6784\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u64a4\u9500\u64cd\u4f5c<\/strong> \u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u901a\u5e38\u901a\u8fc7\u5b9e\u73b0\u4e00\u4e2a\u64a4\u9500\u6808\u6765\u8bb0\u5f55\u6bcf\u4e00\u6b65\u64cd\u4f5c\u7684\u72b6\u6001\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5f53\u9700\u8981\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u4ece\u6808\u4e2d\u5f39\u51fa\u4e0a\u4e00\u6b65\u7684\u72b6\u6001\u5e76\u6062\u590d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u64a4\u9500\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u64a4\u9500\u64cd\u4f5c\u901a\u5e38\u901a\u8fc7\u7ef4\u62a4\u4e00\u4e2a\u64cd\u4f5c\u6808\u6765\u5b9e\u73b0\u3002\u6bcf\u5f53\u6267\u884c\u4e00\u4e2a\u64cd\u4f5c\u65f6\uff0c\u5c06\u8be5\u64cd\u4f5c\u7684\u9006\u64cd\u4f5c\u538b\u5165\u6808\u4e2d\u3002\u5f53\u9700\u8981\u64a4\u9500\u64cd\u4f5c\u65f6\uff0c\u4ece\u6808\u4e2d\u5f39\u51fa\u9006\u64cd\u4f5c\u5e76\u6267\u884c\u3002\u4f8b\u5982\uff0c\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\uff0c\u6bcf\u6b21\u63d2\u5165\u6216\u5220\u9664\u5b57\u7b26\u65f6\uff0c\u53ef\u4ee5\u5c06\u76f8\u5e94\u7684\u9006\u64cd\u4f5c\uff08\u5220\u9664\u6216\u63d2\u5165\uff09\u538b\u5165\u6808\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u6808\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u6808\u662f\u4e00\u79cd\u540e\u8fdb\u5148\u51fa\uff08LIFO\uff09\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u64a4\u9500\u64cd\u4f5c\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6765\u5b9e\u73b0\u6808\u3002<\/p>\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>    def push(self, action):<\/p>\n<p>        self.stack.append(action)<\/p>\n<p>    def pop(self):<\/p>\n<p>        if self.stack:<\/p>\n<p>            return self.stack.pop()<\/p>\n<p>        else:<\/p>\n<p>            return None<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>undo_stack = UndoStack()<\/p>\n<p>undo_stack.push(&quot;Insert &#39;a&#39;&quot;)<\/p>\n<p>undo_stack.push(&quot;Insert &#39;b&#39;&quot;)<\/p>\n<p>print(undo_stack.pop())  # \u8f93\u51fa: Insert &#39;b&#39;<\/p>\n<p>print(undo_stack.pop())  # \u8f93\u51fa: Insert &#39;a&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u5b9e\u73b0\u64a4\u9500\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u64a4\u9500\u64cd\u4f5c\u901a\u5e38\u6d89\u53ca\u5230\u5e94\u7528\u7a0b\u5e8f\u7684\u72b6\u6001\u7ba1\u7406\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6587\u672c\u7f16\u8f91\u5668\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u6808\u6765\u5b9e\u73b0\u64a4\u9500\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class TextEditor:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.text = &quot;&quot;<\/p>\n<p>        self.undo_stack = UndoStack()<\/p>\n<p>    def insert(self, char):<\/p>\n<p>        self.undo_stack.push((&quot;delete&quot;, char))<\/p>\n<p>        self.text += char<\/p>\n<p>    def delete(self):<\/p>\n<p>        if self.text:<\/p>\n<p>            char = self.text[-1]<\/p>\n<p>            self.undo_stack.push((&quot;insert&quot;, char))<\/p>\n<p>            self.text = self.text[:-1]<\/p>\n<p>    def undo(self):<\/p>\n<p>        action = self.undo_stack.pop()<\/p>\n<p>        if action:<\/p>\n<p>            if action[0] == &quot;insert&quot;:<\/p>\n<p>                self.text += action[1]<\/p>\n<p>            elif action[0] == &quot;delete&quot;:<\/p>\n<p>                self.text = self.text[:-1]<\/p>\n<p>    def get_text(self):<\/p>\n<p>        return self.text<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>editor = TextEditor()<\/p>\n<p>editor.insert(&#39;a&#39;)<\/p>\n<p>editor.insert(&#39;b&#39;)<\/p>\n<p>print(editor.get_text())  # \u8f93\u51fa: ab<\/p>\n<p>editor.undo()<\/p>\n<p>print(editor.get_text())  # \u8f93\u51fa: a<\/p>\n<p>editor.undo()<\/p>\n<p>print(editor.get_text())  # \u8f93\u51fa: <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u72b6\u6001\u4fdd\u5b58<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5b9e\u73b0\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c\u7684\u65b9\u6cd5\u662f\u4fdd\u5b58\u6bcf\u4e00\u6b65\u7684\u72b6\u6001\u3002\u5728\u6267\u884c\u64cd\u4f5c\u4e4b\u524d\uff0c\u5c06\u5f53\u524d\u72b6\u6001\u4fdd\u5b58\u5230\u4e00\u4e2a\u72b6\u6001\u6808\u4e2d\uff0c\u5f53\u9700\u8981\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c\u65f6\uff0c\u4ece\u6808\u4e2d\u5f39\u51fa\u4e0a\u4e00\u6b65\u7684\u72b6\u6001\u5e76\u6062\u590d\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4fdd\u5b58\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u5728\u6267\u884c\u64cd\u4f5c\u4e4b\u524d\uff0c\u5c06\u5f53\u524d\u72b6\u6001\u4fdd\u5b58\u5230\u6808\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class StateStack:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.stack = []<\/p>\n<p>    def push(self, state):<\/p>\n<p>        self.stack.append(state)<\/p>\n<p>    def pop(self):<\/p>\n<p>        if self.stack:<\/p>\n<p>            return self.stack.pop()<\/p>\n<p>        else:<\/p>\n<p>            return None<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>state_stack = StateStack()<\/p>\n<p>state_stack.push(&quot;State 1&quot;)<\/p>\n<p>state_stack.push(&quot;State 2&quot;)<\/p>\n<p>print(state_stack.pop())  # \u8f93\u51fa: State 2<\/p>\n<p>print(state_stack.pop())  # \u8f93\u51fa: State 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u5b9e\u73b0\u72b6\u6001\u4fdd\u5b58<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u72b6\u6001\u6808\u6765\u5b9e\u73b0\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class TextEditorWithState:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.text = &quot;&quot;<\/p>\n<p>        self.state_stack = StateStack()<\/p>\n<p>    def insert(self, char):<\/p>\n<p>        self.state_stack.push(self.text)<\/p>\n<p>        self.text += char<\/p>\n<p>    def delete(self):<\/p>\n<p>        if self.text:<\/p>\n<p>            self.state_stack.push(self.text)<\/p>\n<p>            self.text = self.text[:-1]<\/p>\n<p>    def undo(self):<\/p>\n<p>        state = self.state_stack.pop()<\/p>\n<p>        if state is not None:<\/p>\n<p>            self.text = state<\/p>\n<p>    def get_text(self):<\/p>\n<p>        return self.text<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>editor = TextEditorWithState()<\/p>\n<p>editor.insert(&#39;a&#39;)<\/p>\n<p>editor.insert(&#39;b&#39;)<\/p>\n<p>print(editor.get_text())  # \u8f93\u51fa: ab<\/p>\n<p>editor.undo()<\/p>\n<p>print(editor.get_text())  # \u8f93\u51fa: a<\/p>\n<p>editor.undo()<\/p>\n<p>print(editor.get_text())  # \u8f93\u51fa: <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5229\u7528\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u6808\u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u548c\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c\uff0c\u6bd4\u5982\u53cc\u5411\u94fe\u8868\u548c\u5feb\u7167\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u53cc\u5411\u94fe\u8868<\/h4>\n<\/p>\n<p><p>\u53cc\u5411\u94fe\u8868\u662f\u4e00\u79cd\u94fe\u8868\u7ed3\u6784\uff0c\u5176\u4e2d\u6bcf\u4e2a\u8282\u70b9\u5305\u542b\u6307\u5411\u524d\u4e00\u4e2a\u8282\u70b9\u548c\u540e\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u5e38\u6570\u65f6\u95f4\u5185\u8bbf\u95ee\u524d\u4e00\u4e2a\u548c\u540e\u4e00\u4e2a\u8282\u70b9\uff0c\u56e0\u6b64\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Node:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.prev = None<\/p>\n<p>        self.next = None<\/p>\n<p>class DoublyLinkedList:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.head = None<\/p>\n<p>        self.t<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l = None<\/p>\n<p>    def append(self, value):<\/p>\n<p>        new_node = Node(value)<\/p>\n<p>        if self.tail:<\/p>\n<p>            self.tail.next = new_node<\/p>\n<p>            new_node.prev = self.tail<\/p>\n<p>            self.tail = new_node<\/p>\n<p>        else:<\/p>\n<p>            self.head = self.tail = new_node<\/p>\n<p>    def remove_last(self):<\/p>\n<p>        if self.tail:<\/p>\n<p>            if self.tail.prev:<\/p>\n<p>                self.tail = self.tail.prev<\/p>\n<p>                self.tail.next = None<\/p>\n<p>            else:<\/p>\n<p>                self.head = self.tail = None<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>dll = DoublyLinkedList()<\/p>\n<p>dll.append(&quot;State 1&quot;)<\/p>\n<p>dll.append(&quot;State 2&quot;)<\/p>\n<p>dll.remove_last()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u5feb\u7167<\/h4>\n<\/p>\n<p><p>\u5feb\u7167\u662f\u4e00\u79cd\u4fdd\u5b58\u7279\u5b9a\u65f6\u95f4\u70b9\u6570\u636e\u7684\u65b9\u6cd5\u3002\u6bcf\u6b21\u64cd\u4f5c\u4e4b\u524d\uff0c\u4fdd\u5b58\u5f53\u524d\u72b6\u6001\u7684\u5feb\u7167\uff0c\u5f53\u9700\u8981\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c\u65f6\uff0c\u6062\u590d\u5230\u4e4b\u524d\u7684\u5feb\u7167\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Snapshot:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.snapshots = []<\/p>\n<p>    def take_snapshot(self, state):<\/p>\n<p>        self.snapshots.append(state)<\/p>\n<p>    def restore_snapshot(self):<\/p>\n<p>        if self.snapshots:<\/p>\n<p>            return self.snapshots.pop()<\/p>\n<p>        else:<\/p>\n<p>            return None<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>snapshot = Snapshot()<\/p>\n<p>snapshot.take_snapshot(&quot;State 1&quot;)<\/p>\n<p>snapshot.take_snapshot(&quot;State 2&quot;)<\/p>\n<p>print(snapshot.restore_snapshot())  # \u8f93\u51fa: State 2<\/p>\n<p>print(snapshot.restore_snapshot())  # \u8f93\u51fa: State 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5177\u4f53\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>4.1 \u6587\u672c\u7f16\u8f91\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\uff0c\u64a4\u9500\u64cd\u4f5c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u7684\u6808\u6216\u72b6\u6001\u4fdd\u5b58\u65b9\u6cd5\u6765\u5b9e\u73b0\u64a4\u9500\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class TextEditorApplication:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.text = &quot;&quot;<\/p>\n<p>        self.undo_stack = UndoStack()<\/p>\n<p>    def insert(self, char):<\/p>\n<p>        self.undo_stack.push((&quot;delete&quot;, char))<\/p>\n<p>        self.text += char<\/p>\n<p>    def delete(self):<\/p>\n<p>        if self.text:<\/p>\n<p>            char = self.text[-1]<\/p>\n<p>            self.undo_stack.push((&quot;insert&quot;, char))<\/p>\n<p>            self.text = self.text[:-1]<\/p>\n<p>    def undo(self):<\/p>\n<p>        action = self.undo_stack.pop()<\/p>\n<p>        if action:<\/p>\n<p>            if action[0] == &quot;insert&quot;:<\/p>\n<p>                self.text += action[1]<\/p>\n<p>            elif action[0] == &quot;delete&quot;:<\/p>\n<p>                self.text = self.text[:-1]<\/p>\n<p>    def get_text(self):<\/p>\n<p>        return self.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u56fe\u5f62\u7f16\u8f91\u5668<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u5f62\u7f16\u8f91\u5668\u4e2d\uff0c\u6bcf\u6b21\u64cd\u4f5c\u90fd\u4f1a\u6539\u53d8\u753b\u5e03\u7684\u72b6\u6001\u3002\u53ef\u4ee5\u4f7f\u7528\u72b6\u6001\u4fdd\u5b58\u6216\u5feb\u7167\u65b9\u6cd5\u6765\u5b9e\u73b0\u64a4\u9500\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Canvas:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.shapes = []<\/p>\n<p>        self.state_stack = StateStack()<\/p>\n<p>    def add_shape(self, shape):<\/p>\n<p>        self.state_stack.push(list(self.shapes))<\/p>\n<p>        self.shapes.append(shape)<\/p>\n<p>    def remove_shape(self):<\/p>\n<p>        if self.shapes:<\/p>\n<p>            self.state_stack.push(list(self.shapes))<\/p>\n<p>            self.shapes.pop()<\/p>\n<p>    def undo(self):<\/p>\n<p>        state = self.state_stack.pop()<\/p>\n<p>        if state is not None:<\/p>\n<p>            self.shapes = state<\/p>\n<p>    def get_shapes(self):<\/p>\n<p>        return self.shapes<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c\u7684\u529f\u80fd\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u65e0\u8bba\u662f\u64a4\u9500\u64cd\u4f5c\u3001\u72b6\u6001\u4fdd\u5b58\u3001\u8fd8\u662f\u5229\u7528\u6570\u636e\u7ed3\u6784\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u8303\u56f4\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u54ea\u4e9b\u65b9\u5f0f\u64a4\u9500\u4e0a\u4e00\u6b65\u64cd\u4f5c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u76f4\u63a5\u64a4\u9500\u4e0a\u4e00\u6b65\u64cd\u4f5c\u7684\u529f\u80fd\u5e76\u4e0d\u662f\u5185\u7f6e\u7684\uff0c\u56e0\u4e3aPython\u672c\u8eab\u662f\u4e00\u4e2a\u7f16\u7a0b\u8bed\u8a00\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7f16\u8f91\u5668\u6216IDE\u3002\u4e0d\u8fc7\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff08\u5982Git\uff09\u6765\u7ba1\u7406\u4ee3\u7801\u7684\u66f4\u6539\uff0c\u4ece\u800c\u5b9e\u73b0\u64a4\u9500\u64cd\u4f5c\u3002\u6b64\u5916\uff0c\u4e00\u4e9bIDE\u548c\u6587\u672c\u7f16\u8f91\u5668\u63d0\u4f9b\u4e86\u64a4\u9500\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7\u5feb\u6377\u952e\uff08\u5982Ctrl + Z\uff09\u6765\u64a4\u9500\u6700\u8fd1\u7684\u4fee\u6539\u3002\u5bf9\u4e8e\u6570\u636e\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528\u6808\u7ed3\u6784\u6216\u5217\u8868\u6765\u4fdd\u5b58\u64cd\u4f5c\u5386\u53f2\uff0c\u4ece\u800c\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u64a4\u9500\u529f\u80fd\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u6ca1\u6709\u7c7b\u4f3c\u4e8e\u201c\u64a4\u9500\u201d\u6216\u201c\u56de\u9000\u201d\u7684\u5e93\uff1f<\/strong><br \/>Python\u7684\u6807\u51c6\u5e93\u5e76\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u64a4\u9500\u64cd\u4f5c\u7684\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c<code>cmd<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u547d\u4ee4\u884c\u5e94\u7528\u7a0b\u5e8f\uff0c\u914d\u5408\u81ea\u5b9a\u4e49\u7684\u5386\u53f2\u8bb0\u5f55\u673a\u5236\uff0c\u53ef\u4ee5\u652f\u6301\u64a4\u9500\u64cd\u4f5c\u3002\u6b64\u5916\uff0c<code>Undo<\/code>\u6a21\u5757\u4e5f\u53ef\u7528\u4e8e\u7279\u5b9a\u5e94\u7528\u7a0b\u5e8f\u573a\u666f\uff0c\u5e2e\u52a9\u7ba1\u7406\u7528\u6237\u7684\u64cd\u4f5c\u5386\u53f2\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u64a4\u9500\u529f\u80fd\uff1f<\/strong><br \/>\u5b9e\u73b0\u7b80\u5355\u7684\u64a4\u9500\u529f\u80fd\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5217\u8868\u6765\u4fdd\u5b58\u64cd\u4f5c\u5386\u53f2\u3002\u6bcf\u6b21\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u5c06\u5f53\u524d\u72b6\u6001\u63a8\u5165\u5217\u8868\u4e2d\u3002\u5f53\u9700\u8981\u64a4\u9500\u65f6\uff0c\u4ece\u5217\u8868\u4e2d\u5f39\u51fa\u6700\u8fd1\u7684\u72b6\u6001\u5e76\u6062\u590d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">class UndoManager:\n    def __init__(self):\n        self.history = []\n\n    def perform_action(self, action):\n        self.history.append(action)\n\n    def undo(self):\n        if self.history:\n            return self.history.pop()\n        return None\n\n# \u793a\u4f8b\u7528\u6cd5\nmanager = UndoManager()\nmanager.perform_action(&quot;\u64cd\u4f5c1&quot;)\nmanager.perform_action(&quot;\u64cd\u4f5c2&quot;)\nprint(manager.undo())  # \u8f93\u51fa: \u64cd\u4f5c2\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u9002\u5408\u7ba1\u7406\u7b80\u5355\u7684\u64cd\u4f5c\u5386\u53f2\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u5e94\u7528\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u5b8c\u5584\u7684\u72b6\u6001\u7ba1\u7406\u7cfb\u7edf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fd4\u56de\u4e0a\u4e00\u6b65\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528\u64a4\u9500\u64cd\u4f5c\u3001\u72b6\u6001\u4fdd\u5b58\u3001\u4ee5\u53ca\u5229\u7528\u6570\u636e\u7ed3\u6784\u7b49\u65b9\u6cd5\u3002 \u5176\u4e2d\uff0c\u64a4\u9500\u64cd\u4f5c \u662f\u6700\u5e38\u89c1 [&hellip;]","protected":false},"author":3,"featured_media":1123428,"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\/1123418"}],"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=1123418"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1123418\/revisions"}],"predecessor-version":[{"id":1123431,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1123418\/revisions\/1123431"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1123428"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1123418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1123418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1123418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}