Hi everybody,

I have a question about python modules/classes.

When I first started writing an application with pygtk, the tutorial I
had been following at the time instructed me to create a python class
to represent the application. I did so, and it made sense at the time,
when the entire program was contained within one file. But, as my
program grew larger, I added more and more classes to represent
different types of data that I was interacting with, and eventually I
got to a point (at about 2,000 lines of code) where I decided to split
the file up into different modules and combine them in a package.

Now, I'm in a situation where my python package looks something like this:

$ find appname
appname
appname/__init__.py
appname/main.py
appname/datatypes.py
appname/testsuite.py
appname/ui.glade
appname/data.txt

(simplified, but you get the idea: multiple python modules packaged in
a python package).

Anyway, at this point I'm really happy with my "datatypes.py" module.
It contains some classes where it makes sense to have classes (eg,
when you have multiple instances of the classes and instance methods
that manipulate the instance data independently of the other
instances).

But I just can't shake this feeling that having a class to represent
just the GUI when it's already inside of a module is somehow
redundant. There will only ever be one instance of that class. What
good is that? I could just ditch the class and have all the class
methods be top-level module function definitions, and then I could
rename "main.py" into "appname.py", and in my runner script, change
this code:

from appname.main import AppName
AppName().main()

into this:

from appname import appname
appname.main()

Does that make sense? I guess what I'm trying to say is that it seems
to me that the python _module_ could provide the same encapsulation as
the python _class_, except it would do it with simpler code (suddenly,
all the code in main.py has one less level of indentation).

I'd love to hear everybody's thoughts on this issue. Is there some
benefit to keeping my pygtk-specific code in a class to itself? I
certainly saw the benefit of it back when I had multiple class
definitions in the same file, but now the class is defined inside it's
own module and that just makes the whole thing seem redundant.

Another reason why I care about this issue is that method invocations
are slower when there's a period in the name, so eg calling
"self.foobar()" is by definition slower than simply calling
"foobar()", so there's a minor speed boost to be had by ditching the
class and replacing it with all module-global functions.

One thing I changed recently that got me thinking about this is that I
changed GtkBuilder from being an instance variable to a top-level
module variable. So, eg, I changed this:

class AppName:
    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_from_file("ui.glade")
        self.window = self.builder.get_object("window")
        #etc

into this:

builder = Gtk.Builder()
builder.add_from_file("ui.glade")
get_obj = builder.get_object
class AppName:
    def __init__(self):
        self.window = get_obj("window")

And now all throughout my code I've been able to change
"self.builder.get_object('foo')" into "get_obj('foo')". It's so much
shorter, takes up so much less space, and with two fewer periods in
the method name, it's surely much more efficient in places where it
had to call that method a lot. Why shouldn't I continue this trend
with the rest of the AppName class?

Thanks guys!

-- 
http://exolucere.ca
_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to