Sublime Forum

sublime.Selection.__bool__() not behaving as expected


sublime.Selection behaves like a list yet exhibits this behaviour:

>>> sel = sublime.active_window().active_view().sel()
>>> sel.clear()
>>> len(sel)
>>> bool(sel)

Shouldn’t sublime.Selection.__bool__() return False when there are no selections?

I’m using Sublime Text 3.2.2 on macOS.



The Selection.__bool__ method returns an indication that the selection is “valid”; i.e. that it is the selection for a view that actually exists:

def __bool__(self):
    return self.view_id != 0

In the more recent 4xxx builds, the new Python 3.8 plugin host has an augmented implementation of this method that behaves more like you would expect:

def __bool__(self):
    return self.view_id != 0 && len(self) > 0

The legacy Python 3.3 plugin host in this builds still uses the other implementation to be backwards compatible with existing plugins that haven’t been updated for the new host yet.

1 Like


:+1: Great, thanks!