0

I want to have a look at the source code from PyQt5.QtWidgets but end up seeing the code below:

class QPushButton(QAbstractButton):
    """
    QPushButton(parent: QWidget = None)
    QPushButton(str, parent: QWidget = None)
    QPushButton(QIcon, str, parent: QWidget = None)
    """
    def actionEvent(self, *args, **kwargs): # real signature unknown
        pass

    def autoDefault(self): # real signature unknown; restored from __doc__
        """ autoDefault(self) -> bool """
        return False

    def changeEvent(self, *args, **kwargs): # real signature unknown
        pass

There are only some annotations and methods with only 'pass' and 'return', can anybody please explain how they work? Where are the other codes?

  • Those appear to be default implementations meant to be overridden in a child class. They supply dummy implementations so deriving classes don't need to write implementations for methods they don't need. – Carcigenicate Oct 30 '18 at 14:24
  • That isn't the source code of PyQt5. It's just some junk produced by whatever IDE you are using. It has nothing to do with PyQt5 whatsoever. The "other codes" are all in the Qt sources and are entirely written in C++. PyQt5 is simply a thin wrapper around the Qt libraries. It doesn't implement any of the functionality provided by classes like `QPushButton` - it just calls the underlying C++ implementation. – ekhumoro Oct 30 '18 at 17:32
  • @ekhumoro Thanks, so I should just check Qt Documentation (http://doc.qt.io/qt-5/qtmodules.html) for more details? But I'm not very familiar with C++, is there any in Python? – Xiyao Zhou Oct 31 '18 at 02:08
  • @XiyaoZhou What do you want to know? The public API of `QPushButton` is documented [here](https://doc.qt.io/qt-5/qpushbutton.html#). The implementation is all written in C++, so there is no Python code that you can look at either in Qt or PyQt. If you want to know how to use the `QPushButton` class in PyQt, there are hundreds of examples on SO and elsewhere. – ekhumoro Oct 31 '18 at 11:20
  • Thanks, @ekhumoro, that helps a lot. – Xiyao Zhou Nov 08 '18 at 12:34

1 Answers1

1

An abstract class like this one, simply put, is made to be extended by another class which will then implement these empty methods. This is like an interface in software. Python follows principles of Duck-typing, which means certain objects should be implementing methods with specific names, arguments and return types (the method signature), as well as specific properties. So, this abstract class helps maintain these principles.

The module is likely expecting you to extend this class, fill in the methods, and then another object or function in the module used later can expect to make calls on one or more of these three specific methods on your custom button object. This is Duck-typing.

EDIT: This would be true if it were an abstract base class, but instead it's just a noisy class stub auto-generated by PyCharm.

charley
  • 213
  • 2
  • 8
  • It isn't an abstract class - it's just a fake stub class generated by pycharm. – ekhumoro Oct 30 '18 at 17:42
  • @ekhumoro I didn't realize. Should I delete this answer, edit, or just leave it? – charley Oct 30 '18 at 18:24
  • It's up to you, but I don't see any harm in leaving it as it is. Your answer would be perfectly reasonable in many other contexts. My comment wasn't meant to be critical - it was just statement of fact. – ekhumoro Oct 30 '18 at 20:23
  • @ekhumoro Absolutely. I'll leave it as-is, except make a note that it's a Pycharm side-effect. Good to know – charley Oct 30 '18 at 23:34