Do actual atm’s implement classes to store user data?
Yes! Once the software reads the data from disk, it will almost assuredly use classes to keep track of the data. There is an extra layer, though. In most commercial software, there is too much data to store at once so you need to structure the data itself before the program even says it. This is known as a database. Databases store data in a manner that is very similar to classes.
Why did initializer not have to have wins and losses in its argument?
Parameters are pieces of information that a method/function needs in order to complete its job. So, when designing a constructor, we only need to ask for information that, if absent, would make the task impossible. For wins and losses, there is no need to ask for the information since our team starts the season with no games played. They have 0 losses and 0 wins.
Another way to think of it is that a method should be minimal. If you add unnecessary parameters, your class becomes tedious to use. For example, when I want to create several teams:
ncaaf = Team("Wolverines", 2018, "Ann Arbor")
nba = Team("76ers", 2017, "Philadelphia")
nfl = Team("Packers", 2020, "Green Bay")
...
If I add a parameter for wins and losses, I am forcing the user of the class to think about initializing wins and losses to 0.
ncaaf = Team("Wolverines", 2018, "Ann Arbor", 0, 0)
nba = Team("76ers", 2017, "Philadelphia", 0, 0)
nfl = Team("Packers", 2020, "Green Bay", 0, 0)
...
That sure feels like a wasteful effort.
what happens if there’s something wrong with your class and you use it in a program?
Python will produce errors as usual. The stack trace error that Python prints will indicate that the error is in your class file.
Is there a limit to how many parameters a initializer has?
Nope. But the more parameters we add, the more cumbersome it is to use. When we used the graphics library, there were a lot of details that the constructor could have asked for that they chose not to. For example, when creating a Rectangle
, why not require a fill color, outline color, and GraphWin
right away? The designers chose to make a minimal set of requirements and then give additional methods in case you want to customize.
Why do we need to close a file after we open it
The file is locked when we open it - no other program could use it, and if we try to re-open it Python may fail or give strange behavior. Closing the file releases it back to the system so it can be used by anyone.
I am a bit unsure of when to use the self call.
Only within a class definition, and only to refer to data/methods that are part of the object definition. When we come up with your list of data that we want in an object, you will always refer to it with self.variable
.
I am still confused about how all of the methods, classes, and objects are precisely defined.
We’ll practice some more next week! That’s why we added an extra week for classes to the syllabus - its unique.
How do atm’s process large data bases so fast
I will be teaching a whole course about this in the Fall! It takes a lot of ideas that you learn about in CS21/CS35 and adds a lot of discussion about practicality (as opposed to just big-O).
what’s the point of hard coding it?
I assume you mean the tests for Lab 10? Harcoding the test cases allows us to reuse our tests as we update our program. If we don’t hardcode, we have to manually enter our test strings every time we run the program. It also allows me to see that you’ve clearly thought about testing your programs.
The scope of a variable is the object. When you import it into a program, is it then within the scope of the program?
When you create an object, the object has its own scope that follows the normal rule. So, for example, if I have a function foo()
that creates an Account
object:
def foo():
acct = Account("Ameet", 1234567, 1111, 100.01)
acct.calculateInterest(0.02)
#do some stuff
The scope of acct
is the foo()
function - it was created here, and is only available within this function. The scope of the data members of Account
is the acct
variable. Within any method of Account
, I can access that data and it persists even after the method is done. So, for example, when I call calculateInterest
, the balance
variable is accessible since its scope is acct
(which is called self
in the method). Furthermore, my update to self.balance
persists after calculateInterest
is done running - it doesn’t vanish until acct
does.
Do you have to always use Self, can it be called something else? i.e team
It is Python convention to use self
. Yes, you can call it anything you want. No, don’t do that. As a side note, other languages call it different things. Python is the only one that defaults to self
because it was written by hipsters.
Everything makes a surprising amount of sense at the moment!
My dream is to one day say the same thing about quantum mechanics.
Can you create a class out of anything you want?
Sure, next week we’ll rethink old exercises/labs and see if classes improves our solutions.
Is there a limit to the number of methods a class can have?
Not that I know of. Tensorflow, Google’s library for machine learning and neural network, has thousands of methods and functions and keeps growing.
who came up with self?
Probably the same person who though avocado toast was a great idea.