Lecture Materials

Questions & Answers


Q: Will the scores of diagnostic be released in Moday's class?

A1:  live answered


Q: did you get a haircut?

A1:  live answered

A2:  yes


Q: Chris, did you get a haircut?

A1:  live answered


Q: suddenly with that haircut, Chris looks So grown up!

A1:  Sometimes a haircut is all it takes.


Q: What is the grade needed to pass CS106A?

A1:  The university policy to "pass" (which applies to CS106A) is a C- or higher.


Q: is Spotify code freely available on internet to look at like how Chris is describing?

A1:  Unfortunately, I don't believe there code is freely available.


Q: Can you have classes within classes? Or would that not make sense to do?

A1:  I think you would usually define two classes in separate files, and then import both of those into the file where you use both classes.


Q: if we were to see the programs for most games and social networking apps, would we have learned enough up to this point to understand them?

A1:  Yes, for some of them! On Monday, I'll actually show you some of the original code in Google (when it was still a research project at Stanford) and you'll be able to understand it.


Q: Why does simba.bark appear twice in life.py?

A1:  We want simba to bark twice. Chris will trace the program and you'll see what happens.


Q: What exactly is the ‘variable type’ here? The moment you call simba = Dog() ?

A1:  The "type" is a Dog, which means the variable is an object which is an instance of the Dog class.


Q: I heard from my section leader that you learn a new programming language in CS106B. What language is it? Also, are there other classes that focus on more advanced uses of Python?

A1:  CS106B teaches the language C++. CME193 teaches more Python (for scientific programming).


Q: does .__dict__ have a dunder or just one underscore before and after dict?

A1:  It's double underscore (dunder).


Q: if we later defined another class called Cat could we add simba = Cat() or would it say it has to be Dog() since simba was a Dog() before?

A1:  You could overwrite simba to be a Cat -- the same way you could overwrite a variable that stored an int with a float or a string or anything else.


Q: is .append() an example of an instance variable for list?

A1:  .append() is an example of a method for list.

A2:  .append() is a method (function) of a list


Q: Do you define all variables that you want to use in the constructor?

A1:  Most of the time. You can define them in other functions as well. But most of the time they show up in the constructor since they are often initialized there.


Q: How can we look at both files at once in Pycharm like Chris is right now? Thank you!!

A1:  Right click (Ctrl click on mac) the file and then click split vertically


Q: How do you show two files on the screen at once?

A1:  Right click (Ctrl click on mac) the file and then click split vertically


Q: Have we learned enough to be able to create complex programs like Spotify? What would you recommend we do after the quarter so that are able to create such complicated programs in the future?

A1:  You can build many complex programs! In the next assignment you will buid a search engine ! Take CS106B to learn more about how to create complex programs.

A2:  You've learned enough of a foundation that you can continue to build programs and learn even more along the way. Programmers are constantly learning about new programming patterns to build more complex programs.


Q: Why isn't times_barked also a parameter?

A1:  times_barked is an instance variable. It's stored inside the object for the Dog, so you don't pass it in.


Q: Are folders, like the asssignment folders necessary to save these different files, (dog.py and dogs.py) ?

A1:  The files need to be in the same directory or folder.


Q: for biasbars should we use classes to make all the shapes? or try with lists/dicts

A1:  You don't need to use classes for BiasBars. You can just use lists/dicts as a data structure directly in your program.


Q: This is kind of unrelated, but is there a way to create a varying number of variables using a for i in range loop? Like, it you dont know how many variables you are going to need to create, so you can number the variable with i (ie x_i= some value).

A1:  You can create a series of new objects and append them all into a list, for example. That's what create_balls does.


Q: Did Juliette get any ++ tears this Quarter?

A1:  Once, yes! Hoping to see more code that brings tears to my eyes!


Q: Can you decompose across files without using classes? Can you put a method in a new file?

A1:  Yes. You need to import other files if you want to use the functionality in them in another file.


Q: in create_balls(canvas), wouldn’t we need to index ball and make it ball[i] because it will just replace its previous value every time?

A1:  When you append them to the list, you are essentially putting each ball in a new index position in the list.


Q: why are oval, change x, and change y not parameters in the init function?

A1:  These are instance variables so they are defined in the constructor rather than passed in.

A2:  oval, change_x, and change_y are instance variables in the Ball class. That means that those variables are stored inside each object that is a Ball. So you don't pass those variables in as they are already "inside" the ball.


Q: why is canvas passed in as a parameter?

A1:  the ball needs to know which canvas to draw itself on, so canvas is passed in as a parameter. You could actually have multiple canvases as part of a program and want to draw balls in different canvases.


Q: once we create a ball, if the change of x is set to 7, it will always either be 7 or -7 right? It doesn't get a new value later?

A1:  Yes, in this example. But you could imagine adding another method to the Ball class that allowed someone to set the value of change_x or change_y.


Q: how do we decide whether to use _ for a method? can variables also have _ at the beginning?

A1:  variables can have an underscore at the start of a variable name if you want. We use that as a convention to indicate that the variable (or method) is only used "internally" or privately. The double underscores (dunder) are used for special methods in python, like __init__ which are specially understood by Python. Chris also explained a bit more of this live.


Q: If we have two closely related Classes, can we put them into one file. If so, how to we import them into the main file? is it from file import Class1, Class2?

A1:  It is usually convention to give each class its own file, but that is how you would import two classes from the same file.


Q: Can constants from the file with the main function be used in the class file?

A1:  No. The class file doesn't know about the file with the main function that is accessing it. If you want to have a constant that used in a class, you either need to define the constant in the file with the class or in some other file that the class file imports.


Q: Didnt Chris change the color of the balls to red? I wonder what made them blue again when he just played the animation?

A1:  He ran two animations.


Q: Is there way to debug class in python?

A1:  You can debug by using it in the client!

A2:  You can debug a class similarly to debugging other programs.


Q: Until when will all the resources be available on the CS106A website?

A1:  Until the website gets reset by university IT for the Winter quarter version of CS106A. That will happen just before Winter quarter starts.


Q: would both versions (classes and no classes) run with the same efficiency or would one be faster/slower?

A1:  They are pretty similar. Efficiency differences isn't really a concern.


Q: Is if __name__ == ‘__main__’: main() a class? Or does that just run the program?

A1:  It just runs the program. It's not a class.


Q: So a Python library is basically a file of classes with methods that other people can then download and use?

A1:  Yup. In Python we call them "modules".


Q: What is the dunder for?

A1:  It names special names that Python understand do particular things. For example __init__ is understood by Python to be the constructor method for a class.


Q: what can we use the value returned by id(x) for?

A1:  It gives you an id for a variable, so you can check if two variables are, say, referring to the same object in memory.

A2:  You can also learn more about use cases for these addresses in CS106b!


Q: So is the ref count always mutable?

A1:  The ref count is something stored/modified internally by Python. It's not something you directly modify.


Q: Why does Python perform x += 1 by changing the id of the variable instead of changing the value on the heap? Is it more efficient to do so?

A1:  When you do x += 1 and x is an immutable type, then Python has to create a new value in the heap since it can't mutate the existing variable.


Q: if you had x = 5 and y =5 would the ref count be 2?

A1:  Yes.


Q: Is this going to be on Assignment 7?

A1:  Assignment 7 with provide an opportunity to work with classes if you want.


Q: is the recycling of the variable called garbage collections?

A1:  Yes. Garbage collection is the compute cleaning up (reclaiming) memory that is no longer being used for any active variables in the program.


Q: Are there any classes at Stanford that are about the inner workings of computers?

A1:  Yes. After CS106B (which will also talk a bit more about this), you can take CS107, which provide a lot of details about the inner workings.


Q: What is the reference count?

A1:  It's a count of the number of variable that are referring to a particular value/object in memory. When the reference count becomes 0, it means that no variables are refering to that piece of memory, so the computer can reclaim (recycle) that memory.


Q: Should we focus on learning how references work and how a computer works on a ground-level or should we just keep it in mind?

A1:  We encourage you to learn about this because it will help you debug! Get curious! Talk to your section leader. Take CS106b :)