编程语言
首页 > 编程语言> > python-在崇高的条件下,为什么def run在一种情况下可以工作而在另一种情况下不能工作,我如何使其工作?

python-在崇高的条件下,为什么def run在一种情况下可以工作而在另一种情况下不能工作,我如何使其工作?

作者:互联网

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

我在sublime_plugin.TextCommmand的另一个类中工作.

我对运行定义的外观感到困惑.我知道java(十年前做过一些OOP编程,我记得很好),但是我对python的了解很少. (所以我不太了解带有参数的类,因为它不是在Java中,但是我很难猜出它有点像“扩展”-继承-“实现”).

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

请注意,这里的view.run _(‘……’)不适用于blahtest类,它不会打印’aaaaaaaa’

我在控制台中没有任何错误.插件-what.py加载良好.因此,一个类运行方法运行,而另一个不运行. blahtestCommand确实会加载.我可以在def run和blahtestCommand类之间放置一行以打印“ 123456789”,并且只要保存了what.py’cos,它就会立即打印并且没有错误.只是当我查看时它的run方法没有被调用.run_command(‘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"

06001

添加
通过完全奇怪的运气,我设法使它适用于WindowCommand

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"   

我可能会在将来进一步更新有关在sublime api类中运行“ run”的问题.

解决方法:

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

Inheritance in Python

对于#2,Sublime Text 2支持三种类型的命令.运行命令时将调用run方法.除了必需的参数外,您还可以定义要运行的参数.当您运行带有附加参数的命令时,需要在映射中传递这些参数.

> ApplicationCommand:整个Sublime Text 2的命令.不需要参数.
> WindowCommand:窗口命令.没有必需的参数.
> TextCommand:用于查看的命令.一个必需的参数,编辑.

对于#3,如何运行:

> ApplicationCommand:sublime.run_command(‘application_command_name’).检查API reference中sublime模块的run_command函数.
> WindowCommand:window.run_command(‘window_command_name’).检查sublime.Window的run_command方法.
> TextCommand:view.run_command(‘text_command_name’).检查sublime.View的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")

运行以下命令:

>>> sublime.run_command('test_application')
running TestApplicationCommand
>>> window.run_command('test_window')
running TestWindowCommand
>>> view.run_command('test_text')
running TestTextCommand

示例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

标签:sublime-text-plugin,sublimetext2,python
来源: https://codeday.me/bug/20191030/1966706.html