[Tutor] global variables/constants versus volatile variables/constants

Alan Gauld alan.gauld at btinternet.com
Sat Jun 14 23:45:18 CEST 2014

On 14/06/14 13:53, diliup gabadamudalige wrote:

> Say if I have a lot of Lists, strings and variables used to carry data
> to and from various functions why can't I have a class with all these in
> it?

You can but it should also have the functions that operate on the data 
too. Thats the point of classes they link function and data together
so you don't need to pass lots of parameters around. If you have a class 
that jusat holds lots of disparate data items you might as well just use 
a list/tuple or dictionary. The value of classes is when you add 
behaviour or methods to the mix.

> I thought on the lines of blood carrying various things to different
> organs in the body.

But even there the blood has a distinct set of related items, it doesn't 
carry the food to your stomach or the video signals from
your eye to your brain. And blood has operations - it can flow,
coagulate, increase/decrease red-cell count etc. It has
behaviour as well as data.

> class Containers():
>      def __init__(self):
> self.last_selected_scale= ""
> self.scale_notes = ""
> self.arpeggionotes = ""
> self.handplayed_notes = []
> self.MIDIscale = []  # the MIDI scale to be played is here
> self.play_MIDIscale = False
> self.play_MIDIarpeggio = False
> self.play_MIDI_scale_note = False
> self.play_MIDI_arpeggio_note = False
> self.MIDInote_inscale = 0

Now that could be a Tune... And it might have record/play/pause operations

> now say if I play the MIDI keyboard, I collect the notes played which
> will be appended to the list v.hanplayednotes. This will be done from a
> function which will scan the keyboard and append played notes to that list.

But that could be a method of the Tune sub-class, MidiTune, which knows 
how to record from a MidiSequencer object.

> This is how I have used these variables, lists and strings in this
> program. So far it has run error free.

The issue is not how to make it run - you can do that in assembler.
The issue is how easy is it to read and how easy os it to modify and fix 
later. 80% of the cost of commercial software is in "maintenance"
The goal of professional software engineers is to reduce maintenance
costs even if that increases the initial 20% development budget.

> Is this not correct or bad design. I know it is not the normal way of
> using class but is it wrong?

Almost certainly, because it adds a layer of complexity for little or no 

Whereas adding the methods to the class improves the maintainability and 
readability (and sometimes the performance, although that's a secondary 

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list