Note that there are some explanatory texts on larger screens.

plurals
  1. POWays to organize Settings in Sublime Text plugin
    primarykey
    data
    text
    <p>Perhaps it is another round of "static vs instance" but I am trying to be devil in details. Note that I am fairly new to Python and plugin development.</p> <p>Some plugins use the following code:</p> <pre><code>s = sublime.load_settings(__name__ + '.sublime-settings') class Settings: def load(self): self.setting1 = s.get('setting1', 'default1') self.setting2 = s.get('setting2', 'default2') ... # Global Scope settings = Settings() settings.load() s.add_on_change(__name__ + '-reload', settings.load) </code></pre> <p>There is nothing particularly wrong with this example except <code>load_settings</code> and <code>add_on_change</code> methods should belong to <code>Settings</code>:</p> <pre><code>class Settings: def __init__(self): self.settings = sublime.load_settings(__name__ + '.sublime-settings') self.settings.add_on_change(__name__ + '-reload', self.setup) self.setup() def setup(): self.setting1 = self.settings.get('setting1', 'default1') self.setting2 = self.settings.get('setting2', 'default2') ... # Global Scope settings = Settings() </code></pre> <p>Now <code>Settings</code> class encapsulates all functionality. But do we really need instances of this class ? I don't think so. That's why <code>static</code> should be used:</p> <pre><code>class Settings: settings = sublime.load_settings(__name__ + '.sublime-settings') @staticmethod def init(): Settings.settings.add_on_change(__name__ + '-reload', Settings.setup) Settings.setup() @staticmethod def setup(): Settings.setting1 = Settings.settings.get('setting1', 'default1') Settings.setting2 = Settings.settings.get('setting2', 'default2') ... # Global Scope Settings.init() </code></pre> <p>The advantages (if any) of the example above are:</p> <ul> <li>class encapsulates <em>all</em> functionality related to settings</li> <li>no need to create instances to access settings</li> </ul> <p>Are there any other "decent" ways to organize Settings ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload