Why do `props` exist?


I was wondering why do we have to access attributes through .props? Do you think it would be easier to just access them directly? I mean label.text is better than label.props.text, right? So why was it decided to use the props structure?

I even wrote an article talking about how to make it possible to access the attributes without .props.

You can read the reasoning on Bugzilla and the corresponding mailing list thread, but the short version is: this feature was introduced in pygobject a long time ago, but after the bindings had been released. Introducing a whole new set of attributes inside a Python object would have broken existing code that did:

label.text = "Some internal value"

so it was decided to namespace the GObject properties to avoid collisions and breakage.

Your article assumes code is getting newly written and opting into this functionality, but doesn’t take into consideration existing code suddenly getting a whole bunch of attributes that didn’t exist beforehand.

Side note: ideally, you shouldn’t be using the set_{property_name} and get_{property_name} formats. First of all, you’re missing is_{property_name} for boolean, read-only properties; secondly, the introspection data contains the setter and getter functions for GObject properties; it might be hard to extract from Python, but if you have access to the girepository API, you should be able to get the references to the setter and getter methods using g_property_info_get_setter() and g_property_info_get_getter(), respectively.

1 Like

Thank you for all this info : ) Extremely interesting.

I agree that it would work well only for the new apps.

It’s true I forgot about is_{property_name}. But thinking about it, there might be some problems if I would support is_{property_name}. Because for example Gtk.Widget has get_visible() and is_visible() methods, and their behavior is different. So with my implementation if you need the behavior of get_visible(), you can use widget.visible; but if you need the behavior of is_visible(), you can use widget.is_visible(). If I would account for is_{property_name}, then I wouldn’t know how a call widget.visible would translate. Should it become widget.is_visible() or widget.get_visible()?

But thanks for reading my article, and providing me all the useful info and ideas. :smiling_face:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.