[Tutor] Basic Question
dn
PyTutor at DancesWithMice.info
Fri Sep 10 18:05:07 EDT 2021
On 10/09/2021 19.26, Roel Schroeven wrote:
> Op 10/09/2021 om 0:46 schreef Mats Wichmann:
>> Python has a kind of interesting approach to what is global and what
>> is local. I'm sure this will be covered in your material eventually
>> so just quite briefly: if you write a function that just accesses a
>> variable declared not in the function, but at the module level, then
>> that "just works". If you try to change it, then that variable is
>> considered local to the module instead, *unless* you list it as global.
> Small correction in an otherwise excellent post: "... then that variable
+1
> is considered local to the *function* instead".
Pedantic explanation:
Yes, using the combination of terms in one sentence was a bit confusing.
In OO theory, the terminology used for a block of code that fulfils one
responsibility or requirement, is "module". Python uses the specific
term "module" more in the sense of a file. In the same manner, we can
talk (confusingly) about a system's logical-requirement being a
"function" of the system and/or being def[ined] as a "function" in
Python code.
Terminology!?
Way-back, when "modular programming" was revolutionising coding practice
(and before OO was portrayed as a 'silver bullet'), we could discuss
"modules", but implementation almost certainly required a unit such as a
"paragraph", a "section", a "subroutine", or (wait for it) a "function"
- no "module" in sight. (and we think English might be a language for
"communication"?)
Cue joke about how many words (synonyms) Eskimos have for "snow" - and
then someone will come-along and debate the word "Eskimo"...)
Another terminological conundrum?
I've met code where the (senior programmer) writer defends having a
single function which contains:
- establishment code
- a with/context
- a while loop
- some finalisation
... rather than separating those components into four Python
functions/modules.
The claim, is that each of the above code-units (which I could easily
identify/separate by-eye when reading the program!) obeys the SRP, and
therefore doesn't *need* to be located within its own "module".
Theory argues that OO requires "encapsulation" - but that 'requirement'
may be more related to "information-hiding" than functional considerations.
Python's philosophy is to be 'open' when it comes to information-hiding
- there is no real concept of "private". So, the idea of 'open'
modules/functional-units not being formally encapsulated within a
Python function or method would seem congruent.
NB personally-speaking I don't favor their argument, and will stick with
my preference for small/short functions/methods and attempts to become a
classy-guy...
Apparently there is plenty of room to debate which meaning of "function"
to apply, and in varying contexts...
More directly related to Tutor and teaching/learning Python:
Whilst delving into the meta-level of OO theory: I wondered if we spend
a little too much time/effort 'teaching objects'?
Thus, a recently-reviewed book talked of "mammals" as a super-class,
which was sub-classed into "cat" and "dog". It then disappeared into the
relatively-standard discussions of behavior and attribute. Advanced
topics included inheritance cf composition. A fairly standard or common
progression.
However, listening to the OP's needs did make me wonder (again) if we
become so enamored/enamoured with the actual objects and their
characteristics and associated 'rules', that we spend very little
(teaching) time discussing the less-obvious, but entirely essential,
need to pass "messages" between objects - see earlier excellent
description of using arguments/parameters.
We discuss how the cat and the dog both 'walk' in similar fashion, but
how each makes a different 'noise'. How much time is spent illustrating
how the dog indicates that (s)he would like to play, or that the cat
wishes the dog to maintain a certain distance (or else!)?
How does the student indicate an interest in registering for a course,
the purchaser a wish to buy some product, ...
Advanced topics find that word (messages) being re-used?over-loaded with
the likes of MQ (message queuing) and specific techniques related to
multi-processing and API calls.
So, the base concept would seem important when writing one's first code,
and will expand both conceptually and practically, with one's horizons.
Your thoughts?
--
Regards,
=dn
More information about the Tutor
mailing list