Lecture Materials

Questions & Answers


Q: How are we doing today friends!

A1:  It’s too hot here and my laptop is freaking out!


Q: Will we get some practice with object-oriented-programming with our last assignment?

A1:  You will certainly get experience with building a larger scale program. You're going to make a search engine!

A2:  Yep - we haven’t decided whether it’ll be mandatory or an extension but it’ll likely be there!


Q: Is variable type and class the same thing?

A1:  A class defines a new variable type, but some already come built into Python, like ints and lists!


Q: is in this slide the function "bark()" an example of a method?

A1:  Yep!


Q: wouldn't you call bark(simba) instead of simba.bark() ?

A1:  Python fills in the "self" parameter with the object that the method is called on.

A2:  Nope, because it’s a method of the Dog class. It’s just like doing list.append() rather than append(list)


Q: Variables = instances?

A1:  Instances of a class are stored in variables


Q: Can you use other classes while defining a new class? Say if I want to define ball, can I use tkinter in that definition?

A1:  Yep


Q: They can't all use the same variable for oval?

A1:  Nope, because they’re separate ovals on the screen


Q: building off of Bianca’s question… having trouble distinguishing instances and variables

A1:  Instances are objects that are stored in a variable. So the variable essentially refers to the object (which is an instance of a class)

A2:  Instances are actual objects defined using the blueprint that a class provides. Variables just store values (which might be instances of a class)


Q: I have been working on an extension of this exact problem for a few weeks now, where the balls represent molecules. I have used lists within lists, but as the number of 'molecules' increases over 100, the animation becomes kind of chunky (slow). I am wondering if there is a particular collection/data structure which will allow the animation to run in live time faster than others?

A1:  That’s probably not a data structures problem so much as it is just a lot of balls need to be moved before you update the canvas, so it looks a little stuttered. If you’re interested in more sophisticated solutions to this issue, feel free to post on Ed!


Q: so you can have 10 dictionaries in the list balls, all with the same name “ball” ?

A1:  It’s just a list of 10 dictionary addresses - none of them are called ‘ball’ in the list


Q: The 5 and 15 is locking the angle the balls are always moving at?

A1:  It’s a random range between 5 and 15, but yes, those are fixed


Q: How do the balls in the list stay different if they’re named the same?

A1:  The name is just how they’re referred to in the for loop - remember that they’re stored at different addresses, and those addresses are stored in the list

A2:  Items in lists aren't named. The list isn't a dictionary. The list is just storing a bunch of dictionaries.


Q: Is the .update method for ball and canvas different?

A1:  Yep, we’ll show it to you in a sec


Q: so the create.ball is a new class pass from this new program to the main one, right?

A1:  Yep


Q: So there’s no reason to import tkinter to Ball.py?

A1:  Nope, because the canvas has an update method

A2:  You don't need to import tkinter in Ball since you're not directly using functions from tkinter. The functions you are calling are through the canvas.


Q: so python can read from different files at the same time without having to be in the same “window”?

A1:  Yes, Python doesn’t care what you have open in PyCharm


Q: is it possible to define your class in your primary program file? i can see how it would be bad style but would it also malfunction?

A1:  You can do that, but it’s not ideal style


Q: how is each ball variable differentiated?

A1:  Each ball is stored at a different location in your computer’s memory


Q: so calling ball.etc calls that file?

A1:  Yep


Q: so classes are like a more robust function instead of decompsition within a program you decompose across multiple .py files

A1:  Amongst other advantages, yes!


Q: So when you say simba = Dog(), you’re just saying that the simba variable is the dog class?

A1:  An instance of the dog class, yep


Q: does creating juno after simba not overwrite the previous simba dog?

A1:  Nope, it’s just like making two lists


Q: but all the many dogs created just could be called simba and juno, right? or how does it work with for loops

A1:  The name is just how they’re referred to in the for loop - remember that they’re stored at different addresses, and those addresses are stored in the lis


Q: if you did want to make that many dogs with the for loop what would the body look like? like how do you make a different variable for each loop

A1:  dogs = []
for i in range(400):
 dog = Dog()
 dogs.append(dog)


Q: Why don’t you have to define times_barked as a dictionary?

A1:  It’s just an instance variable int inside the class - because it’s a count, it can just be an int and not a dictionary. The __dict__ method makes that dictionary for you

A2:  Python sets up the dictionary to keep track of attributes (instance variables) of an object


Q: Is simba or times barked the instance variable? And what do you call whatever (simba or times barked) is not the instance variable?

A1:  Simba is the instance, and times_barked is the instance variable!


Q: Wasn’t the data for immutable types stored in stack?

A1:  That was a simplification earlier in the quarter; as a matter of fact, in Python, only references are stored on the stack

A2:  The reference (memory address) is stored in the stack. The value is stored in the heap.


Q: weird question: why wouldn't the references (numbers) for x,y,z be sequential?

A1:  Not a weird question at all! The way your operating system allocates memory is not necessarily sequential (or perhaps another program grabbed the memory address immediately after)


Q: does that reference (456…) lives in the ram memory?

A1:  All of this is in RAM!


Q: why is it 0? because it is a new variable?

A1:  The 0 was the reference count, which was how many variables refer to that int. When the 6 is created, no variables are referring to it yet so it has a refcount of 0.


Q: how is using URLs efficient? it seems like you need space for these long tags and space for the variable itself, thats like double

A1:  The answer is it’s not :) Python variables take up a bunch of space, which is one of the reasons that for systems with very little memory, Python isn’t used very frequently. Python errs on the side of making life easier for the programmer

A2:  It can be inefficient. But Python is more focused on ease of use than efficiency.


Q: sorry, I should have been more specific. if I want to see the attributes of ball #5, what variable name would i use to find ball #5?

A1:  If the balls are in a list called "balls", you'd refer to balls[4]

A2:  balls[4]


Q: are reference URLs created randomly, like how does python decide what an address is

A1:  That was supposed to be an ! at the end :)

A2:  Python asks your OS for a memory location and your OS gives you back a memory address with enough space for whatever you’re trying to make?

A3:  There is a memory manager that determines where there is "free" memory to be used. The memory manager gives Python memory addresses that can be used to store new values.


Q: Whats an OS?

A1:  Operating System!


Q: For the __dict__ function with the simba example, what is the ‘times barked’ and ‘2’? Are they the value and ref count?

A1:  live answered


Q: do functions also have special “values” that can be referenced?

A1:  live answered