~ Finally, Python classes make sense!

Python...Again into the rift therein

Today we talked about our 'text adventures'; I had worked on this particular project an entire weekend only to realise I hadn't a clue how to properly utilise Python Classes. They can get a mite hairy.

The choose-your-own-adventure-style assignment was a fairly sandbox-driven task; we had a short list of requirements to fulfill - primarily, the program needed to work.

I took this last portion to heart; I was all-too-aware that my programming skills were non-masterful. I feel lucky to be a part of a coding bootcamp [a la Awesome Inc.](https://www.awesomeinc.org/ "Awesome Inc. Homepage") which selected students with care. To be honest (if not a tad self-deprocating), I am surprised I was chosen along with this group - they are all very smart and creative; everyone is a problem-solver; everyone is interested and dedicated to their tasks.

Okay - enough of that

Classes are tools; they are not only for organising code; in fact, I will go ahead and declare that the organising property Classes assert may be a tertiary or later aspect of their true function.

Classes: Let me explain

After this week, and the several 'pair programming' exercises we have undertaken, I believe that coding well in Python is closely associated with understanding 'Scope' and how that concept can make coding-life a tad bit easier.

I haven't really explained anything yet...have I.

My modus operandum is to assign a new Class for any new category of actions my program may need to perform. This is different than assigning Functions - Functions are logical methods which can be called (time and again) to re-perform whatever action, or checking, which needs be done. A Class seems to set up an environment, for lack of a better word, where Functions can be called; because each Class can be set up with its own localised environment (default everythings), even Functions from outside the Class can be ever-so-slightly modified to do...whatever.

It is important to assign a Class only a few tasks, or responsibilities, to take care of in a program - afterall, the 'creep', or tendancy for super-entangled (spaghetti) code, is difficult to resist. Assigning multiple Classes with more specialised responsibilities allows me to set up an entire infrastructure for my program before getting into the 'running' parts of my program.

Put another way...it is much easier to follow a block of code which is instantiating new Classes, and then calling Class methods, than it is to decypher 100 lines of if/elif/else statements; add that to a myriad of For loops, each with her own local variables (or gods-forbid, ever-changing global variables); add a stack of rogue Functions that must be passed every single variable you create (if the Functions are going to work with one another).

  #The 7th level of Hell.

Published: 2016-09-08