[Python] Remove duplicated definitions of similar arguments in a series of Python functions

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following code,

    class Label(QLabel):
    def __init__(self, *args,
    width:int|None=None,
    height:int|None=None,
    minWidth:int|None=None,
    minHeight:int|None=None,
    maxWidth:int|None=None,
    maxHeight:int|None=None,
    stretchFactor:int=0,
    align:str|None=None,
    color:str|None=None,
    bgColor:str|None=None,
    border:str|None=None,
    fontSize:int|None=None,
    fontWeight:str|None=None,
    fontFamily:str|None=None,
    padding:str|None=None,
    hExpanding=False,
    vExpanding=False,
    name:str|None=None,
    styleSheet:str='',
    labelImg:str|None=None,
    **kwargs):

    super().__init__(*args, **kwargs)

    ss(self, width=width, height=height, minWidth=minWidth, minHeight=minHeight,
    maxWidth=maxWidth, maxHeight=maxHeight,
    stretchFactor=stretchFactor, align=align, color=color, bgColor=bgColor,
    border=border, fontSize=fontSize, fontWeight=fontWeight,
    fontFamily=fontFamily, padding=padding, hExpanding=hExpanding,
    vExpanding=vExpanding, name=name, styleSheet=styleSheet)

    if labelImg is not None:
    self.setPixmap(QtGui.QPixmap(labelImg))



    class Button(QPushButton):
    def __init__(self, *args,
    width:int|None=None,
    height:int|None=None,
    minWidth:int|None=None,
    minHeight:int|None=None,
    maxWidth:int|None=None,
    maxHeight:int|None=None,
    stretchFactor:int=0,
    align:str|None=None,
    color:str|None=None,
    bgColor:str|None=None,
    border:str|None=None,
    fontSize:int|None=None,
    fontWeight:str|None=None,
    fontFamily:str|None=None,
    padding:str|None=None,
    hExpanding=False,
    vExpanding=False,
    name:str|None=None,
    styleSheet:str='',
    onClick:Callable|None=None,
    **kwargs):


    super().__init__(*args, **kwargs)

    ss(self, width=width, height=height, minWidth=minWidth, minHeight=minHeight,
    maxWidth=maxWidth, maxHeight=maxHeight,
    stretchFactor=stretchFactor, align=align, color=color, bgColor=bgColor,
    border=border, fontSize=fontSize, fontWeight=fontWeight,
    fontFamily=fontFamily, padding=padding, hExpanding=hExpanding,
    vExpanding=vExpanding, name=name, styleSheet=styleSheet)

    self.clicked.connect(onClick)


    , and many other similar self-defined widgets like that.

    You can see, so many initial args are the same, it leads to many duplicated code. I want to just define them once.

    But I also want IDEs, like vscode, could inpect the args of those widget classes, so when I input like Label(wid, it could give auto-complete suggestions.

    How to do that?

    Continue reading...

Compartilhe esta Página