1. Classes
I really struggled to understand what a class was and how all of the pieces are put together in a class through methods, in order to create objects. I was working on Assignment 1, and realized that I couldn't proceed with the shaky understanding that I had. I asked my older brother for help, and he provided me with a great analogy. If we have class Car, for example, the cars are going to be our objects that we want to create. We need to put n number of parts in, specified by the __init__ method, which will be put together by the car factory, which is the constructor. The constructor puts the parts together according to the blueprint in the company safe-this is the class. Then, once everything is assembled according to specifications, a Car object is created. Essentially, a class is like a blueprint for creating objects! I did well on the csc108 OOP part of the exam, but I guess I had lost my understanding over the break. I was proactive, and I asked the right person for help, and now I understand it!
2.Inheritance
Surprisingly, I didn't struggle with inheritance. This was actually one of the easier concepts for me to grasp. We needed to use a lot of inheritance in Assignment 1, so I learned it on my own before we learned it in class. When using classes, there are two ways to relate them (that I know of): inheritance and composition. In composition, one class has an instance variable that holds a reference to another class' object. Inheritance is useful when you want to create an abstract class (the parent) and have the children inherit all of its methods, so that new methods can be added if need be later on. Parent and children classes can act in a few different ways: actions on the child create an action in the parent. For example, if you call a method that isn't explicitly defined in the child class, it will look to use it in the parent class. Actions on the child override those of a parent. A method can be overridden by giving it the same name as the parent, and adding any parameters that you want the child to have. Actions on the child can also extend the actions on a parent. In Assignment 1, we did a lot of overriding, because the general game state and strategy classes could not be implemented due to lack of specificity. Therefore, we needed to raise a NotImplementedError, and have child classes of specific games inherit from the general parent classes, in order to override the methods and create specific, functional ones.
3.Class Variables
A variable that is shared by all instances of a class, and defined within a class is called a class variable. However, they are defined outside of any class methods. For example :
class Butternubs(object):
i = 7
>>> Butternubs.i
7
b = Butternubs()
>>> b.i # class variable is accessed as an instance
7
>>> b.i = 3 # assignment to the instance
>>> Butternubs. i # does not change the class variable
7
Instance variables are defined inside a method and are part of the current instance of a class. An instance is the same thing as an object. For example, "17 is an instance of type int", is the same as saying, "17 is an int object". This was a very good dual definition that I found on stack overflow when I was trying to learn more about instance variables. Whenever you have self.something = thing, self is the instance, and something is the instance variable. I will explain this more when I talk about methods.
5. Methods
A method is essentially a function that is a member of a class; it takes a class instance as its first parameter. The difference between a method and a function, is that a method must be called on an instance. We have been creating our own methods, but there are also special methods that we have been dealing with. __str__, __repr__, __init__, and __eq__ are special methods that we have been utilizing a lot, especially in Assignment 1. The __init__ method basically represents a constructor. When you call Foo(), python creates an object and passes it to the first parameter of the __init__ method, which is self, by convention. I believe that __init__ can be excluded if we don't wish to initialize any class variables. The __eq__ method is pretty straight forward. It checks whether or not two objects are equivalent, based on the code that specifies exactly what makes two objects equivalent. For example, it the game state class of assignment 1, two games were equal if certain things about their game states were equal (i.e. whose turn, is the game over , etc.) I struggled with understanding the difference between __str__ and __repr__ for a long time, but I went online to find some resources, and also worked through the assignment, which helped me a lot. Basically, __str__ returns a string representation of what needs to be displayed to the user (the object). __repr__ returns a string representation of self that produces an equivalent object when evaluated in python. In our class Point from lecture, __repr__ and __str__ would output:
>>> p. __repr__()
Point(3.0, 4.0)
>>>p.__str__()
(3.0 , 4.0)
6.Objects
An object is an abstraction of data. If I remember correctly, all objects created in python are stored in the Heap. Objects have identities, types, and values. Basically, everything in python is an object. I am still unsure about one thing: is a class an object? I have been getting mixed answers, and am not sure why. I know that a class contains objects, but this a class can still be an object too, right? Any thoughts are welcome, and are greatly appreciated!
Wel, that's my summary of OOP. I'm sorry about the length; it's a very involved part of programming and I wanted to make sure I covered/understood everything. If anyone has anything to add or correct, please don't hesitate! I like to learn, and know when I've done something wrong!
No comments:
Post a Comment