Keypress Input

Rick Johnson rantingrickjohnson at gmail.com
Thu Jul 16 14:30:04 EDT 2015


On Thursday, July 16, 2015 at 1:09:32 AM UTC-5, Terry Reedy wrote:

> This really is a nice example.  Your rationale for defining an app class 
> is the best I remember seeing.

Well thank you Terry. Your many years of selfless altruistic
offerings to this fine group are both inspiring and
educational. And i can happily admit that you are no doubt
much more of an asset to this group than i.

> >      def evtKeyDown(self, event):
> >          key = event.keysym.lower()
> >          alert = False

Oops. I just noticed that i forgot to remove the "alert =
False" line. It was an artifact from one of my earlier
versions. It's obviously superfluous to we pyhtonistas, but
it could become a source of confusion to the shadow lurkers.

> >          if key == 'r':
> >              self.config(bg='red')
> >          elif key == 'g':
> >              self.config(bg='green')
> >          elif key == 'b':
> >              self.config(bg='blue')
> >          else:
> 
> Can condense block above to this easily extended code: (Replacing if 
> if/elif/elif/... chains, when possible, is part of mastering Python.)
> 
>            try:
>                self['bg'] = {'r':'red', 'g':'green', 'b':'blue'}[key]
>            except KeyError:

Yes you make a good point. And i'm glad you injected this
alternative, as it offers yet another fine "teaching moment" 
for the OP and this group. 

[Warning: Caveat ahead!]

However, i must take exception with your claim that
replacing "classical conditional chains" with Python's
unofficial "pseudo case/switch" is key to: "mastering
Python". I feel there are instances when a "classical
condition chain" are more appropriate, and then other times,
when the "pseudo switch/case" is superior.

For me, I believe the "key to mastering Python" is not to
choose one method over the other (as some sort of religious
rule carved into stone), rather, to know when one method is more
appropriate than the other. That's my person opinion anyway.

> Adding parens to print, when there is a single object
> being printed, has no effect in 2.x and makes the
> statement work in 3.x.  The following works the same in
> both.
> 
> print("Mainloop has returned")

Yes, another fine teaching moment! But just to be clear of
my intentions: I only write code in full 2.x compliance, or
full 3.x compliance. And i do this because i don't want to
inject any "implicit version confusion" into my source code.

In fact, the only time i will mix the incompatible syntaxes
is when i can wrap them explicitly in an exception handler.
But since there is no exception to catch in this case, even
that option would be unavailable to me.

But again. The act of you pointing out this easily
overlooked difference between print 2.x and print(3.x) is
yet another opportunity at exploiting fundamental teaching
moments as they pop up. Hey, who would ever have "thunk" that
teaching could be as exciting and egotistically
fulfilling as a game of wack-a-mole! O:-)



More information about the Python-list mailing list