首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在崇高中,为什么def在一种情况下运行而不是在另一种情况下运行,我如何使它工作?

在崇高中,为什么def在一种情况下运行而不是在另一种情况下运行,我如何使它工作?
EN

Stack Overflow用户
提问于 2013-10-23 03:23:02
回答 1查看 1.4K关注 0票数 3

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

另一门课,我在sublime_plugin.TextCommmand) works上上过。

我对运行定义应该是什么样子感到有点困惑。我知道java(10年前做过一些OOP编程,我记得很清楚),但我对python知之甚少。(因此,我不太了解接受参数的类,因为它不是在java中,但我会很弱地猜测它有点像‘extends’-继承-或'implements')。

我还试图确定ST2 API文档中会告诉某个人,当一个类有sublime_plugin.TextCommand参数时,def运行行应该看起来像这个def run(self, edit),而当一个类有参数sublime_plugin.ApplicationCommand时,def运行应该是什么样子--我不知道是什么样子。(这是一个更大的谜团)

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

我在控制台里完全没有错误。插件- whatever.py正在加载很好。因此,一个类运行方法运行,而另一个类的方法不运行。blahtestCommand确实加载。我可以在def和blahtestCommand类之间划一条线来打印"123456789“,只要我保存了whatever.py‘,它就会打印,因为它重新加载并且没有错误。只是它的运行方法在执行view.run_command时不会被调用。

代码语言:javascript
复制
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(‘但这将工作’) bbbb

加上了奇怪的运气,我设法让它为WindowCommand工作

代码语言:javascript
复制
window.run_command('saef4',{"string":"abcd"})

, {"keys": ["ctrl+s", "ctrl+d"], "command": "saef4", "args": {"string": "abcd"} }

class saef4Command(sublime_plugin.WindowCommand): 
    def run(self,string):
        print "uabcccc"   

今后,我可能会进一步更新有关在崇高api类中运行“run”的问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-23 05:58:30

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

Python中的继承

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

  • ApplicationCommand:对整个崇高文本2的命令。不需要参数。
  • WindowCommand:窗口的命令。不需要参数。
  • TextCommand:视图的命令。一个必需的参数,edit

对于#3,如何运行:

  • ApplicationCommandsublime.run_command('application_command_name').检查run_command函数中的崇高模块在API参考中。
  • WindowCommandwindow.run_command('window_command_name').检查run_command方法的sublime.Window
  • TextCommandview.run_command('text_command_name').检查run_command方法的sublime.View

示例1:没有额外参数的命令

代码语言:javascript
复制
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")

运行以下命令:

代码语言:javascript
复制
>>> sublime.run_command('test_application')
running TestApplicationCommand
>>> window.run_command('test_window')
running TestWindowCommand
>>> view.run_command('test_text')
running TestTextCommand

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

代码语言:javascript
复制
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)

运行以下命令:

代码语言:javascript
复制
>>> 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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19532204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档