wx.TR_HIDE_ROOT problem

Yep, I get that exception. And a question just poped in my mind reading
this:

What is more efficient, using try ... except or testing always to be sure
that no exception is thrown ? Is the overhead of the try ... except block
bigger than that of an if ... else block ? I ask this as the 'if'
condifition will be always checked...

Now I store the root item in a variable self.__root and the root data in
self.__rootData. When I try to get the data I test if the item is self._root
and use the self.__rootData if it is.

So the question is, what's better:

variant 1:

try:
    data = self.GetPyData(item)
except wx.PyAssertionError:
    data = self.__rootData

variant 2 (this is what I currently use):

if item == self.__root:
    data = self.__rootData
else:
    data = self.GetPyData(item)

TIA,

Ionutz

borco@go.ro wrote:

Yep, I get that exception. And a question just poped in my mind reading
this:

What is more efficient, using try ... except or testing always to be sure
that no exception is thrown ? Is the overhead of the try ... except block
bigger than that of an if ... else block ? I ask this as the 'if'
condifition will be always checked...

I don't know but expect that the try/except would be better.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

borco@go.ro wrote:

What is more efficient, using try ... except or testing always to be sure
that no exception is thrown ? Is the overhead of the try ... except block
bigger than that of an if ... else block ? I ask this as the 'if'
condifition will be always checked...

As the Zen of Python ("import this") says, it's easier to ask forgiveness than permission. That is to say, an 'if' statement will incur overhead every time, whereas a try/except will only incur overhead when a specific condition fails.

Generally, I'd be less worried about overhead, and more worried about what makes for clear and concise code. This can be a matter of taste, but I'm inclined to lean towards try/except here, as well. Compare:

     if object.has_feature():
         object.use_feature()
     else:
         object.workaround()

vs

     try:
         object.use_feature()
     except FeatureError:
         object.workaround()

I find the second to be cleaner... and it *also* covers cases where object has the feature, but it fails under usage for some reason.

Jeff Shannon
Technician/Programmer
Credit International