Lecture Materials

Questions & Answers

Q: Can you post the lecture slides please?

A1:  Yes they will be posted shortly!

Q: How should we decide whether to take the class for a letter grade or credit/no credit?

A1:  Thats a good question, though without a general answer. The credit / no credit has a slightly higher bar for passing, though it wont impact your GPA

Q: Just want to double check, we were allowed to use notes + the class site for the diagnostic right?

A1:  yes

Q: In the assignments, I usually didn't change the comments already provided. Does the software account for that?

A1:  No that is fine :)

Q: Will we be notified if our assignment is retracted? If there is a mistake in the retraction process, will we be notified?

A1:  The retraction is your choice. If you chose to retract, we will make sure that your request goes through

Q: so object is opposite of imperative programming?

A1:  thats right!

Q: Will we be allowed to utilize OOP for the challenge?

A1:  certainly!

Q: Were the images in the image program objects?

A1:  Yes they are. Good insight

Q: what is a paradigm?

A1:  “A way of approaching a problem”

Q: When working on assignments, are we allowed to look up reference/syntax for Python? What online resources are acceptable under the honor code?

A1:  You can certainly look up what a python function does. If you find online resources that solve the problems we give you in assignments then that is across the line.

Q: Isn't objectifying wrong?

A1:  Haha. Yes it is! But the CS meaning of “objectification” is pretty different.

Q: So, SimpleImage was a class?

A1:  Yes!

Q: Wait, so if we can use methods with strings, are strings also objects?

A1:  they are! So are lists…

Q: what is benefit of using a class if we were already able to solve various problems without it?

A1:  You can solve even more problems with classes! Perhpas you want to store data of a certain class in multiple different programs. You can use classes to do this!

A2:  They let you define a new variable type! That is super useful :)

Q: What is the class of the objects: string, int, and list?

A1:  They each have their own class! string, int, and list!

A2:  those are each classes! There is a string class, an int class and a list class. You can test this yourself using type >>> x = "this is a string" >>> type(x) <class 'str'> >>>

Q: While we can't overload methods, can we have multiple constructors outside of the default constructor in Python?

A1:  You cannot define multiple constructors. However, you can define a default value if one is not passed. def __init__(self, some_param=“Default”):

Q: So we can have two (or many) objects of Counter with two different values of ticket_num?

A1:  yes!

Q: so we dont need the main() function anymore? the init function is new main() since its the one declaring the variable?

A1:  init is the constructor functon for a class. You would need main in the use_counter file in this example.

Q: When retracting an assignment, does it have to be the whole assignment or could it be certain parts of the assignment?

A1:  Typically its the whole assignment — but if you are retracting a warmup problem we might consider only retracting part

Q: Can you specify the instance variable in the functions instead?

A1:  Yes! You can specify instance variables outside the constructor.

Q: can you create multiple objects in one class?

A1:  Oh yea! I would say you create “multiple objects of one class type”

Q: so the ticket_num is a global variable? how can next_value() access it?

A1:  In a class, all functions have access to the instance variables.

Q: Why is this new code not in a class? Isn't that imperative programming and not OOP?

A1:  There are two parts. In one part you define the new variable type (that is OOP), in the other part you use it (that is imperitive)

Q: if we want to talk privately with chris or mehran about our assignement, is that possible?

A1:  Come to office hours?

Q: Just to be sure, is the use of an external python reference library (such as https://www.w3schools.com/python/python_reference.asp) okay for assignments?

A1:  okay!

Q: Does the fact that strings and lists are objects have anything to do with their mutability/immutability?

A1:  not much. Some classes are immutable, some are mutable. The ones you define in cs106a will be mutable

Q: Is there inheritence/polymorphism in Python?

A1:  There is inheritence, but not polymorphism

Q: whoa, so even a string or int inside a class is mutable?!

A1:  Not quite. If a class has a string, that string value is still immutable

Q: Are there abstract classes or interfaces in Python?

A1:  Not in the same way that there are in java or c++. Python has a separate concept called “duck typing” which serves a similar purpose.

Q: for the contest, is there are only 2 winners for the whole CS106A class?

A1:  Yes, though there have been circumstances in the past where a special “peoples choice” award has been given

Q: How are immutable classes created if the objects we make are all mutable?

A1:  Interestingly, python doesn’t expose an easy way to defined your own immutable class!

Q: Are classes like making a helper function just outside of the main program?

A1:  Thats one of the reasons people use classes! Though they also define new variable types which is helpful in other ways

Q: Just wanted to double check, were we allowed to check online Python reference/Syntax (does not contain any solutions) during diagnostic? Thank you!

A1:  Yes! You were allowed to look up syntax for python functions during the diagnostic so long as the solutions that you wrote were your own.

Q: Was there Chris’s group hours this Monday?

A1:  No, they were cancelled this week.

Q: Are we allowed to use global variables in a class, then?

A1:  they aren’t global variables, the are called instance variables! They are attached to the object — but the instance variables are accessible among all functions of that class which is pretty great

Q: For the contest, which would be better (more likely to win): a. A really cool and beautifully aesthetic program but imports tons of libraries and doesn’t really utilize raw python concepts. or b. A not as cool program but written in pure raw python.

A1:  thats so hard to answer. You should make the greatest program you can… if you judge it to be wonderful, the SLs probably will too

Q: How do you store a class? (Say, in a Json file?) Do you have to retrieve all the individual pieces of information from the class?

A1:  You store the class in a .py file in the same folder and then import that class file into the .py file where you use the calss.

Q: If you set count1 and count2 = 0 in main() would the result be the same?

A1:  You could have recreated this functionality with integers. Classes can do many things ints cant, but this was just a simple demo to show the mechanics. Good question.

Q: Where is the next_value function coming from? Is it in the class?

A1:  it is! Any “method” defined in a class is callable on an object of your variable type

Q: Why would you want to use a class instead of helper functions?

A1:  Style! Why would you want to define your very own variable type?

Q: When will the diagnostic results be released?

A1:  Early next week!

Q: Should we use class in biasbar assignment?

A1:  You do not need to use classes in biasbar

Q: I’m not sure I understand, so in this example, is the value stored in count1 of type counter and it’s not of type integer?

A1:  count1 is type Counter not of type integer. But count1.next_value() gives back an int.

Q: How can you make a parameter optional? (Like in string.find() for instance?)

A1:  Here is a great explainer: https://www.geeksforgeeks.org/default-arguments-in-python/

Q: what lectures are needed to complete assignment 6?

A1:  Everything up to last Friday

Q: so will the method always be changing the variable you created in the constructor?

A1:  doesnt have to change an instance variable.. but it can

Q: Why did he say the filename needs to be lowercase but then wrote out Classname.py with a capital C? Are all characters in the name supposed to be lowercase?

A1:  Perhaps a typo. Often the files are lowercase

Q: Can you define a class in the same .py file along with all the other code?

A1:  you can, but its customary to write them in their own file

Q: Hi! I was just wondering if an IG was planned for occur for Assignment #7?

A1:  No IGs for assignment 7.

Q: Is self the python equivalent of this in Java?

A1:  Yes they are similar.

Q: will there be a final?

A1:  Nope!

Q: Would is the advantage of using OOP instead of just setting count1 = 0 in the main and then changing it from there?

A1:  You could have recreated this functionality with integers. Classes can do many things ints cant, but this was just a simple demo to show the mechanics. Good question.

Q: so what if we need to pass any other param to next_value() will it still need self?

A1:  yes! You have self, then your additional params

Q: What if self.ticket_num was not initialized at the time of construction? will the counts still happen?

A1:  no! That was necessary

Q: I thought you didn’t have to return the object because python already knows? Why did he return in the class function?

A1:  you can optionally return anything you want in a class function (aka a method)

Q: because we might have trouble focusing today, when can we expect to see the recording posted?

A1:  I hear you! You can find them here: https://canvas.stanford.edu/courses/124529

A2:  We will post it later this evening!

Q: so where does the instance variable live? heap or stack from previous lectures?

A1:  Great question! Its in the “heap”

Q: So what does __init__ = "__main__" mean?

A1:  I think you are referring to this code: if __name__ == '__main__': main() It says: when you run this python file, call the main function.

Q: So returning in a method isn’t necessary?

A1:  not necessary

Q: what’s the difference between an object and a regular variable?

A1:  nothing, all variables in python are objects… but in this case the objects are of a variable type that *you* get to define

Q: if we write up code with the help of a teacher, and use that code in our assignment, is that okay?

A1:  A section leader is fine! If it was a teacher outside of the course staff you should have credited them: http://web.stanford.edu/class/cs106a/handouts/honorCode.html

Q: What are the benefits of using OOP over imperative? How do we know when to use what?

A1:  OOP lets you write really large problems. Its the next level of decomposition

A2:  You can use OOP when you want to define a new variable type!

Q: what's the difference between heap and stack again?

A1:  Its optional cs106a material. Its the two places in memory where your variables live — stack is where variable names live, heap is where their values live.

Q: Will Chirs hold his Group hours sometime this week since we missed it this Monday?

A1:  We are going to pick up again next monday at the normal time!

Q: Didn’t Mehran need good grades to get INTO grad school though so therefore they matter???

A1:  From what I have observed, grad school is often more about showing you have done some cool projects than good grades

Q: so UNITS_TO_GRADUATE is still called a global variable even if its inside a class?

A1:  its called a “constant” but it is visible to everything in that file

A2:  UNITS_TO_GRADUATE is a constant

Q: So what’s the relation between def __init__ and if __name__ == ‘__main__’? Is there a relation?

A1:  they are different, but they use similar syntax (the dunder). __name__ == ‘__main__’ says: this is what happens when you run this function file. __init__ says: this is what happens when you create an object of this type.

Q: do you need the get name function? Could you not do name = self.name?

A1:  self.name is an instance variable, not a function. But you are correct, you can use that syntax to get the name of a student

Q: so what is special about the dunder how do we know when to use it in our method name or not?

A1:  only use it for “special” functions. In cs106a this will often just be just __init__. Later you will learn about __str__. There are a few others, but I would focus on them later

Q: How does the get_name function know the names already? Where is it getting the info to return?

A1:  It is getting the information from the instance variable stored in self.name

Q: Is there a way to autogenerate accessors/mutators for instance variables in a class? Or do we have to type all of those methods manually?

A1:  I am not sure if pycharm has this autogeneration feature.

Q: does this program interact with user in any way? does it ask the user what their student ID is? how many units they have taken?

A1:  This will happen in the stanford.py file.

Q: can you have multiple constructors that take in different parameters? (i.e. primary and secondary)

A1:  no! But the init function can take “optional” parameters

Q: so if strings and lists are classes how come they don't need their own py files for us to use them

A1:  Since they are so helpful, they are inherent to python. So just by having python you have access to this information.

Q: Are classes always created in separate files?

A1:  They do not have to be, but it is good style to create them in separate files.

Q: for assn 6 till what lecture do we need?

A1:  Lecture 19

A2:  Lectures up to and including last Friday’s lecture

Q: Can you get units earned by using s.units_earned in the main function?

A1:  that does work…

Q: Do you need to define every parameter as self.variable = parameter before you can use it in your class?

A1:  yes

Q: Can you create immutable objects?

A1:  python actually makes this truly hard! Its not a native ability

Q: In real-world programming, will we use constants as often as we do in this class? For example, I imagine that CS106A_UNITS would be stored in a gigantic file full of course:units pairs, or something similar? Should we expect to see many constants?

A1:  actually putting constants in a file is very popular for larger programs! Good insight

Q: just curious if the chat for the whole class was disabled for a reason?

A1:  I think its enabled?

Q: where can I find all the information regarding the def of objects, instances, variables, heaps, stacks etc.?

A1:  live answered

Q: What’s the paradigm for thinking of a concept like Class in the first place?

A1:  Try to think about it in terms of nouns and verbs. Nouns become your classes/objects and verbs become methods in those classes.

Q: can you post more practice questions for finding bugs in given code?

A1:  live answered

A2:  Sure!

Q: i like your haircut chris :D

A1:  Haha thanks!

Q: oh, for me the chat only goes to panelists. it makes class more fun and social.. not a big deal tho

A1:  Interesting! We’ll check into that next time :)

Q: Just curious, in what occassions should we use object and class?

A1:  Style! Why would you want to define your very own variable type?

Q: Can Mehran elaborate on why grades don’t matter for undergraduates??

A1:  live answered

Q: can Katie talk about ethics for elections and ballot counting etc?

A1:  Go to her office hours and ask about this!!!

Q: What would best enable a student to get into the CS coterm program?

A1:  Come to office hours!

Q: so while class is the blueprint an instance is an object that is built from a class and contains real data?

A1:  Yes, that is correct!

Q: I dont see Katie’s OH on the http://web.stanford.edu/class/cs106a/oh.html

A1:  They are on Ed!!

Q: What does a + or a +checkmark on hw? I'm trying to figure out if I'm passing the class.

A1:  You are doing well!!