My goal here is to avoid having to wright multiple lines to set a widget. What’s wrong with using multiple lines to initialise a widget?
Equivalent without functor (this is what I want to avoid) I don’t have 40 years of experience, but frankly that example is a lot easier to read IMO. Have you considered subclassing? buttonA = MyPushButton() class MyPushButton(QtWidgets.QPushButton): def __init__(self, text, parent=None): super(MyPushButton, self).__init__(text, parent) self.setObjectName("myPushButton") self.resize(60, 20) On Sat, 17 Aug 2019 at 12:36, Rudi Hammad <rudiham...@gmail.com> wrote: > Okey, so this is another design that I am testing. This time I took a > functor aproach (or at least that is what I think I did). > So I came up with this. What do you think, am I overthinking it? > Cheers, > R > > from PySide2 import QtWidgets, QtCore > > class WidgetFunctor(object): > > def __call__(self, widget, **kwargs): > self.widget = widget > for key in kwargs: > self.setTitle(kwargs[key]) if key == "title" else None > self.setObjectName(kwargs[key]) if key == "objectName" else > None > self.setSize(kwargs[key][0], kwargs[key][1]) if key == "size" > else None > > return self.widget > > def setTitle(self, title): > self.widget.setText(title) > print "setting widget title success!" > > def setObjectName(self, objectName): > self.widget.setObjectName(objectName) > print "setting objectName success!" > > def setSize(self, width, height): > self.widget.resize(width, height) > print "setting parent success!" > > # And so on with more methods... > > > # Using the functor > widgetFunctor = WidgetFunctor() > buttonA = widgetFunctor(QtWidgets.QPushButton(), title="foo", objectName= > "buttonA_btn", size=(60, 20)) > buttonB = widgetFunctor(QtWidgets.QPushButton(), title="luu", objectName= > "buttonB_btn")) > comboBoxA = widgetFunctor(QtWidgets.QComboBox(), objectName="comboA_btn", > size=(40, 50)) > > > # Equivalent without functor (this is what I want to avoid) > buttonA = QtWidgets.QPushButton() > buttonA.setText("foo") > buttonA.setObjectName("buttonA_btn") > buttonA.resize(60, 20) > buttonB = QtWidgets.QPushButton() > buttonB.setText("luu") > buttonB.setObjectName("buttonB_btn") > comboBoxA = QtWidgets.QComboBox() > comboBoxA.setObjectName("comboA_btn") > comboBoxA.resize(40, 50) > > > -- > You received this message because you are subscribed to the Google Groups > "Python Programming for Autodesk Maya" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to python_inside_maya+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/898d0237-0aed-4d2c-adcf-c4783ac5fe24%40googlegroups.com > <https://groups.google.com/d/msgid/python_inside_maya/898d0237-0aed-4d2c-adcf-c4783ac5fe24%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group. To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOC_0VovA6PDj-eSKz9qRfdFWzej0Cvm9N4wvFa6vHK3mA%40mail.gmail.com.