Newbie question: Classes

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Jan 9 10:13:50 CET 2008


Daniel Fetchinson a écrit :

nb: answering to the (unknown) OP:

>> Basically, I have created a program using tkinter without using any class
>> structure, simply creating widgets and functions (and finding ways around
>> passing variables from function to function, using global variables etc).

One of the points of OO is indeed to avoid excessive use of global state 
  and "cargo" data. Now note that this can also be (at least partially) 
done using closures, callbacks, partial application, and other 
functional tricks.

>> The program has become rather large ( lines?) I am trying to now put it into
>> a class structure, because I hear it is easier to handle.

It can make thing easier - from a maintainance/reusability POV at least 
- if you're at ease with OO design and programming. But getting OO right 
- specially when it comes to design - is not necessarily trivial 
neither, so don't think of it as a "silver bullet".

>> So basically, I put all the stuff into a class, making the widgets in the
>> "def __init__(self, root)" (root being my Tk() ) and then I have had to put
>> a "self." in front of any instance of any variable or widget. Is this right?

Probably not.

>> it seems like nothing is any easier (except having variables locally). Is
>> this right? Should I be creating more classes for different things or what?

Probably, yes. You could try to identify small sets of functions dealing 
with a common (small) set of data, and regroup them into (small) 
classes, then think about how instances of these classes will work 
together. Also remember to separate the "logic" (data structures and 
rules about these data structures) from the "presentation" (the GUI 
code). The "logic" part should be usable independantly (with a CLI 
interface, a web interface, etc).

My two cents...



More information about the Python-list mailing list