rainerf / ricodebug

ricodebug is a debugger featuring data visualization, similar to DDD
12 stars 9 forks source link

Usage of super in ptyhandler.py #5

Open giselher opened 12 years ago

giselher commented 12 years ago

As said in the meeting the code is written with old style classe in mind and super should be only be used in new style classes.

Link to the code line: https://github.com/rainerf/ricodebug/blob/master/src/helpers/ptyhandler.py#L33

Use cases for super from the python 2.7 Documentation (http://docs.python.org/library/functions.html?highlight=super#super)

There are two typical use cases for super. In a class hierarchy with single inheritance, super can be used to refer to parent classes without naming them explicitly, thus making the code more maintainable. This use closely parallels the use of super in other programming languages.

The second use case is to support cooperative multiple inheritance in a dynamic execution environment. This use case is unique to Python and is not found in statically compiled languages or languages that only support single inheritance. This makes it possible to implement “diamond diagrams” where multiple base classes implement the same method. Good design dictates that this method have the same calling signature in every case (because the order of calls is determined at runtime, because that order adapts to changes in the class hierarchy, and because that order can include sibling classes hat are unknown prior to runtime).

rainerf commented 12 years ago

For Python 3 compatiblity, the whole codebase should be converted to use new-style classes. This should not be too hard to do, I hope.

giselher commented 12 years ago

I still prefer to use usual inherit method instead of super, because it not only looks nicer but you don't have to worry about the downsides of super.

For reference:

rainerf commented 12 years ago

New-style classes have been introduced in Python 2.2, released in 2001 (!), and old-style classes will be removed with 3.0. Searching the web, I could not find any real downsides of new-style classes, other than some people using them incorrectly and some more or less constructed examples involving multiple inheritance. Is there anything I am missing?

The use of new-style classes seems to be encouraged over old-style classes, see for example:

giselher commented 12 years ago

It is fixed in this commit: f7fa7a17d3fabe1847c20054a13749f37b0fb870

rainerf commented 12 years ago

As a short-term solution, plugins/Navigation/NavigationPlugin.py still needs to be fixed as it includes the same construct as ptyhandler.py did.

For the longer-term solution, I still think that new-style classes are the way to go. super()'s caveats seem to be very much tied to multiple inheritance, which should not be a problem in our case.