Search code examples
pythontypespyqtqwt

What is the benefit of having many different data types?


What is the benefit of using many different data types in a language? I have been working with pyqt and pyqwt recently and I keep finding myself typing lines like this:

grid.setPen(Qt.QPen(Qt.Qt.gray, 0, Qt.Qt.DotLine))
curve.setSymbol(Qwt.QwtSymbol(Qwt.QwtSymbol.Ellipse,
                              Qt.QBrush(),
                              Qt.QPen(Qt.Qt.black),
                              Qt.QSize(5, 5)))

What would be lost if the above lines were instead the following?:

grid.setPen('gray', 0 ,'dotted')
curve.setSymbol('ellipse', 'k', (5,5))

Even if the specific types are needed for some technical reason, couldn't the strings be converted to those types inside the method that needs them?

Is this because pyqt and pyqwt are simply bindings for C++ libraries? If so why are they needed in C++?


Solution

  • There are a few benefits:

    1. Type safe languages (like C++) have the advantage that many errors are found at compile time (as opposed to run-time). That means using Qt.grey (note the e) would fail to compile because the type is not defined.

    2. Better performance and less memory. Behind the scenes Qt.gray is a number, and number operations are much faster than strings.

    In the pyqt case, because the library wraps a C++ library it looks more like c++ than like Python code.