- Podcasts
- The Python Show
- 58 - Python Memory Graph with Bas Terwijn
About this episodeOn this week's Python Show, we have special guest Bas Terwijn, author of memory_graph, a Python package similar to Python Tutor that you…
On this week's Python Show, we have special guest Bas Terwijn, author of memory_graph, a Python package similar to Python Tutor that you can run locally on your machine and use for much larger Python applications than Python Tutor can support.
Learn more about memory_graph here:
* memory_graph teaching examples
* memory_graph online web tool
Get full access to The Python Show at www.pythonshow.com/subscribe
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
58 - Python Memory Graph with Bas Terwijn
Yeah, so I'm an educator at the University of Amsterdam in the Netherlands. There I teach many courses in the program Artificial Intelligence. It's a bachelor program. So these are students that just got started at university. And I'm helping them get the grips on Python and also some math and some machine learning applications.
Yeah, that's a long time ago. I started early. A friend of mine had a computer, really interesting toy. So it was at first playing many games. But later we figured out you could cheat in these games by sort of writing a little program. And slowly, step by step, we got sort of an idea of what programming is. And then, yeah, we liked it. We made our own games at some point, text adventures, that sort of really simple things. And yeah, I really liked it. So I chose it later on for higher education.
After I finished my education at Hogeschool Utrecht, I did a... Graduation project at the University of Amsterdam. Writing something with neural networks was kind of new for me. Later on, I got a position there to help students program robots. So this was a robot football competition. Internationally, universities would compete. And yeah, later I got more and more into AI applications, but I'm not an AI expert, although I teach it at like the first or second year bachelor level.
No, not at this level of the course. So later on, students will be taught more advanced tools or PyTorch, I think. But in this course, we stick with the basics still. So we want students to program with matrices low level. You really have an idea what's going on instead of just calling some API and having all the things done for them. That makes sense.
Yes, yes. Yeah, I wrote it as a sort of, it was inspired by Python Tutor, which is a really common tool in Python education. And this is about visualizing how Python works internally. So Python uses reference semantics. So every variable is a reference to a value. And if you assign one value, If you change one variable to another, basically you have two references pointing to the same value. So if you change one of the variables, you also change the other. And this can be confusing, especially for people coming from a A value semantics language like C, C++. Most of the time, if you write A is B, then automatically a copy is made. But in Python and many other languages, you get aliasing or two references pointing to the same value. This can be confusing for students. And Python Tutor is a tool that's, I think, 15 years old or something. It's really good at visualizing these references so students really get to understand what's going on internally. Yeah, my memory graph tool is inspired on that because Python Tutor has some limitations that I wanted to get rid of and we did. So now, yeah, you don't only, so Python Tutor can only run in a browser with really small programs. It's a really short runtime, whereas the Memory Graph tool runs locally, so you have full control over what happens, so you can run any program you want.
Yeah, you can choose. So PDF is the default, but you can generate PNG or SVG or many other image formats that just give you a graphical representation of all the And that includes the call stack. So if you call different functions, each function call has its own stack frame of local variables. They all get shown with the references. So students get a good understanding of what's going on.
That sounds really difficult to visualize conceptually, but I know I've taken a look at your product and I think it looks really cool. So I'm going to tell my listeners, go check it out because it sounds weird when we're just talking about it, but if you see it in action, it makes a lot of sense.
And it used to be that the teachers would draw these on the blackboard. This is also what Python Tutor also references a lot. So instead of drawing it now by hand as a teacher, show your students how the internals work. The tool does it for you and you can just debug your software using the visualization. And yeah, you don't have to rely on your teacher to make the drawing fun.
Okay, cool. That clears that up. So when you're working on this kind of a product, what kind of lessons were you learning? What new things did you learn? Whenever I do something brand new, I'm like, this is going to be easy, or this is going to be really hard, and I learn all kinds of fun new things while I'm working on it.
You know, I did learn a lot. And this was for me the first project that got really attention from users, because I had a lot of software before, but it was usually a research project. So only for internal use, maybe one or two other people would play with my software to do their research. But now with this software, the user group is growing. We're getting close to 100,000 users, although it's a bit hard to measure. But yeah, we get traction. Many users are enthusiastic. So that was really new for me. And also writing public software, because you publish an API. And later on, if you decide maybe something should be changed, You really have to consider if you want to do that because people already might be working on this API. Yeah. Yeah. So, yeah, that was really new for me.
The most are from America, but it might also be that there are many bots from America, like indexing the internet or something. But anyway, there's a large group from the USA. Also from India a lot, because it's a large country. And also the south of India is really into programming, mainly the south, I've learned. And also Europe, my own country, because many people see my LinkedIn page. But Germany, France, United Kingdom, these are the main countries.
To make a little list, we can walk through them in one second. Yeah, a quick overview is this Python Tutor only allows you to make one file and with memory growth you can have multiple files. The amount of memory you can take is limited because if you send a Python program to Python Tutor, it runs your program on a remote server and then sends you the results back. So for them, it would be a problem if people would send in used programs because then they would have servers running a lot of programs, whereas memory graph runs locally on your own computer. So it's not limited in any way by a limit on memory use or CPU use. Memorycraft also only use built-in types because in this remote server, not all the packages are installed there. Reading and writing to files is difficult with Python Tutor or maybe not even possible. Reading command line arguments. And also memory graph can be used quite nicely in IDEs, for example, Visual Studio Code and PyCharm. You can also use it in Jupyter Notebook or IPython or other environments. And in Python, Tutor is really only available on a web interface.
But don't get me wrong, it's really a nice tool. Python Tutor is used for more than 15 years now. Many educators know it and use it a lot. It made a really big difference in learning people how to program, especially when it comes to references, aliasing, difference between shallow and deep copy, all kinds of things that are really kind of difficult to explain in words. But if you make an image, it's much easier for students to understand what's going on and to debug their own programs when they see things are not correct.
Yeah, I've used it a little bit, but I haven't used it for teaching purposes just to kind of play around with. So I'm looking forward to playing with your product more or your package more because like you said, it has all this nice stuff I can run locally and I can trade out some of my My bigger programs, see what happens.
Yeah, do let me know if you have any feedback. There's all a problem if you run it on really big programs because then the graph gets really big. And a really big graph, yeah, it's no longer very readable. So also for Python Tutor, this only works really nicely on small programs. Although in memory graph, I have built in some tricks where you can sort of reduce the graph. If you know where you want to search, then you can sort of take out parts of the memory you're not interested in visualizing. But I haven't come up with a good user interface to do this yet. So that can only be done programmatically. And that's not the best interface. So it's something to do still.
Yeah, and there's two ways to use MemoryGraph. One is locally on your machine, and I also have built a web interface because it's just easier for students to get it installed. You don't have to install anything. Just click a link and you can run MemoryGraph. And there, because it's in a web interface, there you cannot use tools like TKinter or PyCharm or other things. Oh, yeah. So there we are still limited, like Python Tutor is also in that sense.
I would like to expand the visualization to other languages. So now it's only Python. And Python Tutor is also strong in the sense that they don't only cover Python. We can also use it for C, I think Java, JavaScript, and I probably forget one language. At least it does a couple of these languages. So I would also like to try to expand my way of doing it to languages like C and C++. These are the first on my list, but maybe other languages in the future too.
Well, still, that sounds really exciting. I'm curious to see how that turns out. While I don't write in C and C++, I still am exposed to that code base. The code base is with the use it a lot. So it'd be kind of fun to be able to visualize it more and understand what's going on under the covers.
Oh, yeah, yeah. I have some spare time, definitely. I like to do some road cycling, only now it's winter, so I haven't really kept that up in the last months. But that's one of my hobbies. I also play a little bit of chess, not at any level, seriously, but just for fun with friends sometimes. Oh, yeah. I'm watching a lot of YouTube. Is that a hobby? Probably.
Yeah. Well, you know, when people talk about, I go for a run, sometimes that means they just run around the block or, you know, they ride to the post office or back. And other people mean, you know, they ride all day and that's what they do for their weekends. You know, I'm just curious what that meant.
But cycling is a great sport because you just sit. It's a seated sport like chess. You can pedal as hard as you want. So you mentioned running. I also did some running before. And that's much harder in the sense that you cannot take it easy for. It's harder to take it easy to recover a little bit, let's say.
