[Python] Python multiple inheretance with property

Discussão em 'Python' iniciado por Stack, Julho 31, 2025.

  1. Stack

    Stack Membro Participativo

    Consider the following code:

    from typing import Any

    from PyQt6.QtCore import pyqtSignal
    from PyQt6.QtGui import QFont
    from PyQt6.QtWidgets import QApplication, QWidget
    from qtconsole.rich_jupyter_widget import RichJupyterWidget


    class BasePane:
    """Represents the common aspects of a pane."""

    def set_font_size(self, new_size: int = 10) -> None:
    """Set font size."""
    raise NotImplementedError

    def set_theme(self, theme: str) -> None:
    """Set theme."""


    class JupyterREPLPane(RichJupyterWidget, QWidget, BasePane):
    """
    REPL = Read, Evaluate, Print, Loop.

    Displays a Jupyter iPython session.
    """

    on_append_text = pyqtSignal(bytes)

    def __init__(self, theme: str = "day", *args: Any, **kw: Any) -> None: # noqa: ANN401
    """Initialise the Jupyter REPL pane."""
    super().__init__(self, *args, **kw)
    self.font = QFont("Helvetica")
    self.console_height = 10
    self.set_theme(theme)

    def _append_plain_text(
    self, text: str, before_prompt: bool = False
    ) -> None:
    """Ensures appended text is emitted as a signal with associated bytes."""
    super()._append_plain_text(text, before_prompt)]
    self.on_append_text.emit(text.encode("utf-8"))

    def set_font_size(self, new_size: int = 10) -> None:
    """Set the font size for all the textual elements in this pane."""
    self.font_size = new_size
    font: QFont = self.font
    font.setPointSize(new_size)
    self.font = font
    print(font)

    def setFocus(self) -> None:
    """
    Override base setFocus so the focus happens to the embedded _control
    within this widget.
    """
    self._control.setFocus()


    app = QApplication([])
    pane = JupyterREPLPane()
    pane.set_font_size(12)


    For reference, font inherited from RichJupyterWidget:

    font = property(_get_font, _set_font)


    font inherited from QWidget:

    def font(self) -> QtGui.QFont:


    It runs fine (Python 3.13):

    <PyQt6.QtGui.QFont object at 0x7f62121de650>


    Then my first question: Is simply using self.font a good practice?

    And if I change the code to:

    def set_font_size(self, new_size: int = 10) -> None:
    ...
    font = RichJupyterWidget.font
    ...


    Then an error ocurrs:

    Traceback (most recent call last):
    File "/Data/mu/test_multiple.py", line 61, in <module>
    pane.set_font_size(12)
    ~~~~~~~~~~~~~~~~~~^^^^
    File "/Data/mu/test_multiple.py", line 47, in set_font_size
    font.setPointSize(new_size)
    ^^^^^^^^^^^^^^^^^
    AttributeError: 'property' object has no attribute 'setPointSize'


    So, is there any simple syntax that can solve this problem? Similar to calling font inherited from QWidget:

    QWidget.font(self)


    The only one I could think of:

    RichJupyterWidget.font.__get__(self)


    It doesn't seem like a graceful way to use properties.

    Continue reading...

Compartilhe esta Página