Are all the screensavers on the cs computers fractals? Are they student made for a lab latter in the cs track?
They are not student made, although that is a cool project idea. There are several built in to the system that are randomly selected from; some of them must be fractals but I haven’t looked into the source to verify.
When making a graphic that continues to zoom in, like the leaf, does the program have to making new fractals as it zooms, or does it do it all at once? and would this at any point begin to overload the program, or are fractals forgotten when they leave the screen?
Yes, it would overload at some point unless we forgot information (i.e., calculated the information on the fly during the zoom). In most cases, we define the “levels” of recursion - how much detail do we need? While fractals, the mathematical concept, are infinitely defined functions, it is not computationally necessary for us to do that.
Are calling classes immutable?
Classes, by default, are mutable. There are ways to make a class mutable (I know how to do it in C++ or Java; I’m not sure what the Python syntax is). In most cases, we want to have objects that are dynamic.
What are other cases in which implicit parameters would be useful?
The only implicit parameter is the self parameter, which has a particular purpose. There are optional parameters, which have default values but can be specified. For example, we could change the Circle class to take an optional color:
def __init__(self, centerPt, radius, color="white"):
...
Color is optional, so if I don’t specify it the default is white. But if do want to specify the color, we can do so:
whiteCirc = Circle(Point(50,50), 25)
redCirc = Circle(Point(100,100),25,"red")
How will we apply classes and objects in the next few weeks?
We’ll create objects that can coordinate/encapsulate lots of data inside of one object. We’ll go through a bank account class, but also consider alternate ways to have implemented previous labs. Your lab assignment will be to design a e-reader that creates Book objects
Can you have a recursive class?
Yes! That is a bit advanced for now, but linked lists is a strategy for storing data recursively.
Can you save classes you created to a library?
Yes! The Graphics library is a set of classes someone defined and gave as a library. In Python, a library is just a Python file that we can read.
Why do we write “self” as the first parameter instead of simply writing the name of the method?
We do have to write the name of the method, but self is the first parameter of the method. It is necessary for the method to know which particular instance of the object is being called. For example, imagine we write a program that creates three Circle
objects (e.g., circ1
,circ2
,circ3
). If I call circ1.setFill("red")
, we know that we are trying to fill circ1
. But in the setFill
method, it needs to know that it is specifically coloring in circ1
. We use self
to refer (or point to) circ1
so that the method knows which object it is acting upon.
If you have multiple objects then how does the self parameter know which one you want to reference?
In the example above, Python maps circ1
to self
automatically. Hence, it is implicit because the person calling the method never needs to define the self
parameter.
What is the difference between class and type?
The type of any object is its class. Not all data is an object, though (ints, floats, bools, etc.).
What are getters and setters?
Methods for accessing the data (e.g., circ.getCenter()
) inside of an object vs changing the data inside the object (e.g., setFill()
or move()
or append()
)
What does the dot notation for the methods of the class look like?
ls.append(x)
- ls
is the object, append()
is the method. The dot in the middle specifies that it is a method that is part of the list
class.
What is the benefit of abstracting code from the user?
Programs would be unusable without abstraction. Think about any piece of software you use, or any website. You don’t need to know how to program to use them. Your iPhone does not include an instruction manual on understanding Swift code, because it has been abstracted away with buttons that you can push.
Our programs should do the same. Do you need to know how to communicate with the terminal to get user input? What gets the program to pause? How do you make the cursor blink while the user waits? How do you know when they hit the Enter key? Because of abstraction, you don’t need to know those answers. You just need to know how to call input()
and the person who wrote the input()
function handled all of those details for you.
how do you know which functions should be methods vs. universal functions?
That’s a good question! The idea behind encapsulation is that anything that describes the class should be a method. For example, when thinking about withdrawing money from an account, we could define that function externally. But withdrawals are an inherent action to accounts. It needs to work internally with the data inside the Account to e.g., verify the pin, balance, and withdrawal amount. append()
is a method instead of a function because it is defined solely for the purpose of modifying a list.
Are there any limitations of methods?
Not really; they can do the same things as functions.
How does all of the account info stay connected?
The Account class defines all of the data the belongs to an object. Python the creates a data frame (similar to a stack frame) where the data resides.
Could a method in a class be used in another class?
Not directly. There are exceptions for when a class builds upon another class, but that is a very advanced usage.