sebp / PyGObject-Tutorial

Tutorial for using GTK+ 3 in Python
https://python-gtk-3-tutorial.readthedocs.io/
GNU Lesser General Public License v2.1
393 stars 161 forks source link

Should we use self.set_title(title="...") instead #200

Open osamuaoki opened 3 years ago

osamuaoki commented 3 years ago

Hi,

I just send the PR #199. I was conservative.

It may be even nicer to change all instances from:

super().__init__(title="...")

to even cleaner style:

super().__init__()
self.set_title(title="...")

This example is more expandable for user in future. Just a thought ...

sebp commented 3 years ago

It's not a big difference, IMHO. I think it's clear what the code's intent is. I would leave it as it is.

osamuaoki commented 3 years ago

For me, if subclassed, code it as really subclassed .. that was the thought.. (So I am not so keen on all the super() things, after all. Please read on.)

Let me re-remind you that one code was in python2 style. layout_listbox_example.py at:

        super(Gtk.ListBoxRow, self).__init__()

Considering the reason behind adopting the syntax change on super() with PEP-3135, at least, we need to get rid of this old python2 syntax code in tutorial document. https://www.python.org/dev/peps/pep-3135/#rationale

Maybe I am wrong. But this and the following are what I saw today by poking upstream documents which seems to demand more style changes.

As I learn more on coding in Python, subclassing seems to be a bad idea these days for overhead etc.

The example code by the upstream pyobject developer doesn't use subclass with __init__ like this tutorial.

Gtk.Template codes, of course, need class but no __init__ used like ones in other parts of this tutorial..

As a tutorial, complicating code by subclass may not be desirable thing if it has clear merits. For example, Section 6.1.1 Examples can be as simple as the following without class:

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

def on_button1_clicked(widget):
    print("Hello")

def on_button2_clicked(widget):
    print("Goodbye")

win = Gtk.Window()
win.set_title("Hello World")

win.box = Gtk.Box(spacing=6)
win.box.set_homogeneous(True)
win.add(win.box)

win.button1 = Gtk.Button(label="Hello")
win.button1.connect("clicked", on_button1_clicked)
win.box.add(win.button1)

win.button2 = Gtk.Button(label="Goodbye")
win.button2.connect("clicked", on_button2_clicked)
win.box.add(win.button2)

win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

Although, we are more likely to use Gtk.Template, it is very educational how each Gtk components are used.

I thank your effort which gave me good heads up. Thank you.

sebp commented 3 years ago

I agree that using Python 3's super() is a good idea, but is unrelated to this.

I'm not sure what you are proposing at this point, get rid of keyword argument in super calls, or sub-classes in general?

I don't think PyGObject is outlawing sub-classes in general, because it would essentially discard the object-oriented nature of Python. I think what they are saying is that overriding methods can be problematic.