Using real world functions to teach arg passing
I'm becoming increasingly aware that the flood of new Python users we're enjoying has everything to do with articles such as this one in Atlantic Monthly last month: https://www.theatlantic.com/science/archive/2018/04/the- scientific-paper-is-obsolete/556676/ I've appended a couple quotes from it. I enjoyed another perspective on the "Mathematica versus Open Source" face-off (Python's ecosystem in particular) in my joining what had been a Mathematica-focused company (Netmath) that then morphed into a Python teaching company (the O'Reilly School of Tech.). I joined after the Netmath chapter, but the purchased company was still working to make Mathematica a more popular front end by developing a product named Hilbert. https://www.cnet.com/news/oreilly-taking-mathematica-online/ Hilbert did the job it was supposed to, and we offered some calculus, but that's not where the market turned out to be. People were flooding into Python, still are. These days I'm all about what the article talks about: Jupyter Notebooks and the various tools it works with. Here's my latest thinking: When first introducing argument passing to functions, along with defining function parameters, using * and ** in particular (as scatter-gatherers) that's the right time to start using some of these fancy 3rd party packages like matplotlib and numpy, for your examples. Your average Python student is like a grad student in physics or statistics. They have lots of STEM skills and just want to get on with it efficiently, without getting side-tracked into investing a huge number of hours in a computer language (approximately the same user profile Guido encountered at Stichting Mathematisch Centrum -- ABC users -- i.e. the language attracts those for whom it was designed). If you get to such as numpy.random.random and matplotlib.pyplot.hist right away (with the appropriate abbreviations), that's like fresh air, oxygen, what they've come for in the first place, so why delay? Sure it's fine to show a couple functions like this for starters: def F(*args, **kwargs): print(args, kwargs) ... but then visit the docs and start showing actual function signatures of the Pyladies and such are *really* using. Build a dict of arguments and pass it with **. """ Three ways to say the same thing """ import numpy as np import matplotlib.pyplot as plt from inspect import signature # some data x = np.arange(0,10) y = np.random.randint(0, 5, size=10) plt.subplot(311) plt.plot(x, y, 'go--', linewidth=2, markersize=12) plt.subplot(312) plt.plot(x, y, color='green', marker='o', linestyle='dashed', linewidth=2, markersize=12) plt.subplot(313) params = {'color':'green', 'marker':'o', 'linestyle':'dashed', 'linewidth':2, 'markersize':12} print(signature(plt.plot)) plt.plot(*(x,y), **params) Side topic: some Python instructors think "keyword arguments and parameters" is a bit confusing given we also have what're called the "Python keywords" (in keyword.kwlist -- except not async or await for some reason, even in 3.6, I guess because local to a namespace?). They prefer "named arguments". However that suggests positional arguments are "unnamed" which isn't quite right either. If there's no *args to scoop up positionals first, dicts work to populate 'em, by name: In [147]: def F(x, y, *, z): ...: print(x, y, z) In [148]: kwargs = {'y':10, 'x':'A', 'z':100} In [149]: F(**kwargs) A 10 100 Even though we don't typically build dicts simply for the sole purpose of passing named arguments in our matplotlib or numpy tutorials, I think we should do that more in classes where core Python is still a focus. In other words, don't "save the best for last". Start using the best right from the top, to teach Python basics. Everyone can relate to a histogram or two, perhaps drawn at random from some normal distribution. Or plot real data why not? Keep it topical in some way. [ The data don't have to be dismal either (so much of stats tries to be "meaningful" in ways I consider less important than using a realistically complicated API). ] I'm working on moving in this direction for my courses in May. In June, I'm hoping to teach high school and middle schoolers how to use Jupyter Notebooks in the context of introducing what I call Martian Math. If you've been on this listserv for awhile, you know it's a pet topic of mine. We'll be using Anaconda on Windows. Kirby PS: I won't be making it to Cleveland this year, but I'm vicariously a participant in living close to where This is the Bus -- a Pythonista-modified school bus ala Ken Kesey that will feature in one of the Cleveland Pycon talks -- is starting from. Here's its picture, with more next to it if you click in Flickr: https://flic.kr/p/JcUt3E
From Atlantic Monthly:
Python became a de facto standard for scientific computing because open-source developers like Pérez happened to build useful tools for it; and open-source developers have flocked to Python because it happens to be the de facto standard for scientific computing. Programming-language communities, like any social network, thrive—or die—on the strength of these feedback loops. ... "I think what they have is acceptance from the scientific community as a tool that is considered to be universal,” Theodore Gray says of Pérez’s group. “And that’s the thing that Mathematica never really so far has achieved.” There are now 1.3 million of these notebooks hosted publicly on Github. They’re in use at Google, Bloomberg, and NASA; by musicians, teachers, and AI researchers; and in “almost every country on Earth.”
participants (1)
-
kirby urner