[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Newbie question: Classes

Daniel Fetchinson

1/8/2008 9:31:00 PM

> 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).
> 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.
>
> 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?
> 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?


Use the method that works best for you. If you like the procedural
approach more then don't worry about being object oriented. The good
thing is that python is multi-paradigm so if custom objects don't make
your life easier then just forget about them :)
2 Answers

Mike Driscoll

1/8/2008 10:14:00 PM

0

On Jan 8, 3:31 pm, "Daniel Fetchinson" <fetchin...@googlemail.com>
wrote:
> > 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).
> > 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.
>
> > 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?
> > 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?
>
> Use the method that works best for you. If you like the procedural
> approach more then don't worry about being object oriented. The good
> thing is that python is multi-paradigm so if custom objects don't make
> your life easier then just forget about them :)

One great benefit to classes is the ability to take a generic class
and then subclass it. Or the ability to instantiate various objects
from one class.

Say you have a dog class. If you pass in the correct data, you can
instantiate a dalmatian, a Labrador or a mutt. They're all dogs, but
their all different too.

Enough of my babbling. Check out the following links for more info:

http://www.freenetpages.co.uk/hp/alan.gauld/tu...
http://www.diveintopython.org/object_oriented_framework/defining_cl...
http://docs.python.org/tut/n...

Mike

Bruno Desthuilliers

1/9/2008 9:14:00 AM

0

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...