python - Sublime Editor 插件记住变量

标签 python plugins sublimetext2

我正在编写一个 sublime editor 2 插件,并希望它在 session 期间记住一个变量。我不希望它将变量保存为文件(它是一个密码),但希望能够重复运行命令,并且可以访问变量。

我希望我的插件像这样工作......

import commands, subprocess

class MyCommand(sublime_plugin.TextCommand):
  def run(self, edit, command = "ls"):
    try:
      thevariable
    except NameError:
      # should run the first time only
      thevariable = "A VALUE"
    else:
      # should run subsequent times
      print thevariable

最佳答案

实现此目的的一种方法是将其设为全局变量。这将允许您从任何函数访问该变量。 Here是一个需要考虑的堆栈问题。

另一种选择是将其添加到类的实例中。这通常在类的 __init__() 方法中完成。一旦实例化类对象,就会运行此方法。有关 self__init__() 的更多信息,请参阅 this stack discussion .这是一个基本示例。

class MyCommand(sublime_plugin.TextCommand):
    def __init__(self, view):
        self.view = view # EDIT
        self.thevariable = 'YOUR VALUE'

这将允许您在创建后从类对象访问此变量。类似这样的 MyCommandObject.thevariable。这些类型的变量将持续到关闭调用该方法的窗口。

关于python - Sublime Editor 插件记住变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15615273/

相关文章:

python - 使用IP地址在python中查找位置

python - 使用 autorun 运行 python 应用程序时出现编码问题

android - 运行 phonegap 相机示例时出错

java - 如何从 gradle 更新 Shadow 插件?

java - 使用Nutch如何抓取使用ajax的网页的动态内容?

html - 从 HTML 中获取 CSS 类

python - pvmismatch 的语法。如何更改特定单元格?

navigation - Sublime Text 2 : disable document preview

ruby-on-rails - Sublime Text 2 中 rails 的正确缩进

python - 机器学习中回归的局限性?