在sublime中,为什么def run在一种情况下有效,而在另一种情况下不起作用,我如何使其工作?

2024-10-03 21:25:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类blahtestCommand(sublime_plugin.ApplicationCommand)运行,它失败了。在

另一个类,我有sublime_plugin.TextCommmand)作品。在

我对run定义应该是什么样子有点困惑。我知道java(10年前做过一些OOP编程,我记得很清楚),但我对python知之甚少。(所以我不太了解一个类接受参数,因为它不在java中,但我会猜测它有点像“extends”-继承-或“implements”)。在

我还试图确定ST2 API documentation中的内容将告诉某人,当一个类具有参数sublime_plugin.TextCommand时,def run行应该是这样的def run(self, edit),而当一个类有参数sublime_plugin.ApplicationCommand时,def运行应该是这样的-我不知道是什么。(所以这是一个更大的谜团)

注意这里的view.run_('......')不适用于blahtest类,它没有打印'aaaaaaaa'

我在控制台中没有任何错误。插件-随便吧。py装好了。因此,一个类运行方法运行,而另一个类的运行方法不运行。我可以在def run和类blahtestCommand之间加一行来打印“123456789”,保存后它就会打印出来随便吧。py因为它重新加载,没有错误。只是我没有调用它的run方法view.run_命令('blahtest')

import sublime, sublime_plugin

class blahtestCommand(sublime_plugin.ApplicationCommand):
    def run(self):
        print "aaaaaaaaaaa"

class butthiswillworkCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        print "bbbb"
>>> view.run_command('blahtest')
>>> view.run_command('butthiswillwork')
bbbb

添加 我运气很好,设法让它为WindowCommand工作

^{pr2}$

关于在sublimeapi类中运行“run”,我可能会在将来进一步更新这个问题。在


Tags: 方法runselfview参数def错误java
1条回答
网友
1楼 · 发布于 2024-10-03 21:25:37

对于1,你是对的。在Python中,这意味着继承。派生类定义的语法类似于class DerivedClass(BaseClassName):。在

{a1}

对于#2,Sublime Text 2支持三种类型的命令。当您运行命令时,run方法被调用。除了必需的参数之外,您还可以为run定义任意多个参数。当运行带有额外参数的命令时,需要在映射中传递这些参数。在

  • ^{}:整个崇高文本2的命令。没有必需的参数。在
  • ^{}:窗口的命令。没有必需的参数。在
  • ^{}:视图的命令。一个必需的参数,edit。在

对于#3,如何运行:

  • ApplicationCommandsublime.run_command('application_command_name')。检查API reference中的sublime模块的run_command函数。在
  • WindowCommandwindow.run_command('window_command_name')。检查^{}run_command方法。在
  • TextCommandview.run_command('text_command_name')。检查run_command方法^{}

示例1:不带额外参数的命令

import sublime, sublime_plugin

class TestApplicationCommand(sublime_plugin.ApplicationCommand):
    def run(self):
        print("running TestApplicationCommand")


import sublime, sublime_plugin

class TestWindowCommand(sublime_plugin.WindowCommand):
    def run(self):
        print("running TestWindowCommand")


import sublime, sublime_plugin

class TestTextCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        print("running TestTextCommand")

运行以下命令:

^{pr2}$

示例2:具有额外参数的命令

import sublime, sublime_plugin

class TestApplicationCommand(sublime_plugin.ApplicationCommand):
    def run(self, arg1, arg2):
        print("running TestApplicationCommand")
        print("arg1: " + arg1)
        print("arg2: " + arg2)


import sublime, sublime_plugin

class TestWindowCommand(sublime_plugin.WindowCommand):
    def run(self, arg1, arg2):
        print("running TestWindowCommand")
        print("arg1: " + arg1)
        print("arg2: " + arg2)


import sublime, sublime_plugin

class TestTextCommand(sublime_plugin.TextCommand):
    def run(self, edit, arg1, arg2):
        print("running TestTextCommand")
        print("arg1: " + arg1)
        print("arg2: " + arg2)

运行以下命令:

>>> sublime.run_command('test_application', {'arg1' : '1', 'arg2' : '2'})
running TestApplicationCommand
arg1: 1
arg2: 2
>>> window.run_command('test_window', {'arg1' : '1', 'arg2' : '2'})
running TestWindowCommand
arg1: 1
arg2: 2
>>> view.run_command('test_text', {'arg1' : '1', 'arg2' : '2'})
running TestTextCommand
arg1: 1
arg2: 2

相关问题 更多 >