Learn Python and what? How to teach about Context Managers (a proposal)

Suppose you got this question: "I'm new to programming and heard that studying two languages, one front burner, one back burner, is a best practice, as the benefits you get from 'compare and contrast' is like when rubbing two sticks together: you get fire." I've been suggesting learning Python (front burner) and... Go (interfaces vs types, slices, concurrency, garbage collected, compiled) Rust (no garbage collection, immutability the default, learns from Python) Clojure (LISPish, big on immutability, targets JVM) J (successor to APL, very different, uses "parts of speech" -- as in grammar -- as shoptalk) JavaScript (humongous stacks and frameworks, Node world, great learning tools) what else is especially apropos I wonder. Of course we run up against SQL and regexes in the course of teaching plain vanilla Python. Ditto HTML/CSS (more punctuation to learn in your ordinary language class, e.g. English or French grammar? -- I call these "over the back fence technologies" in the Youtube below). ------ On another topic, as an instructor I wonder how to introduce context managers as a topic and my best answer is: Take advantage of the sqlite3 module in Standard Library and show a context manager like: with DB("ufo_reports") as db: db.useful_tool( ) where DB is like: import sqlite3 as sql class DB: def __init__(self, dbname): self.dbname = dbname + ".db" def __enter__(self): # make the connection, could be to any SQL engine... self.conn = sql.connect(self.dbname) self.curs = self.conn.cursor() return self # <-- to take advantage of related tools def useful_tool(self): # have a number of methods geared for working with this # specific database, up to you how generic we're trying to be # more useful tools go here def __exit__(self, *oops): if self.conn: self.conn.close() if oops[0]: # error handling # yadda yadda return False # or True return True I yak about all this on Youtube. https://youtu.be/29p3Ckr8SOA Kirby
participants (1)
-
kirby urner