This weeks slog is on object oriented programming concepts in python. Some of these concepts we used in tutorial and our assignment were: classes, class variables, instance variables, instances & instantiation, inheritance, methods, constructors, and objects.
A class is an abstract data-type, which contains information about the data stored in a class and the operations used on such data. As we move away from functional programming towards object oriented program, we use classes to group together data that is related and operations (methods) that we use to manipulate or access the data. Classes have instructions as to how the data is used and attributes the data takes. Classes are used to create objects, and instantiate instances. A class is like the definition, while an object is a type of data being represented and the instance is the specific object that has been created in memory.
When I mentioned data stored in a class, this refers to variables: class variables and instance variables. The distinction between class variables and instance variables is that class variables are defined in the class but outside methods, while class variables are defined in methods themselves. In practice, we identified variables by reading through a description of a class and choosing which attributes and actions would become the variables and methods of our class (e.g. on the example on the test with undergrad and grad students, their grades, classes, and graduation requirements).
Instances are created in instantiation. There are multiple ways of doing this and a proper way using the __init__ constructor method. It is possible to create an object and make a new instance with only three lines of code such as in the point example:
class Point: pass
p1 = Point()
Here we have a class, Point, and an instantiation of point p1. The proper way to instantiate a point required the implementation of __init__. The __init__ method is also often used to create class variables. Class Point has variable coord, class Stack has variable _data, and class Shape has variable position. Observe that in Stack, _data is different from Point and Shape because the underscore _data means that it is not accessible to the public user interface. This draws on the OOP concept of data encapsulation and data hiding. This serves to prevent conflicts between data attributes and method attributes. These special attributes cannot be changed except through special methods get() and set(). We briefly discussed this during week 3, but we have not had to use this in our assignment. We did, however, use inheritance, a concept related to class/instance variables but not quite the same.
In this course, inheritance has so far mainly been dealing with subclasses. Inheritance is basically how subclasses/child classes derive data and behavior from its super class/parent class. To inherit the attributes of a parent class, we would write, for example, Square(Shape). Subclass methods can also override methods of their parent class if the parent class method is implemented to raise an error stating that the method should be implemented in the subclass. We used this frequently in our first assignment and will be using it in our second. We could have coded subtract square without using subclasses (we could even have coded it without using object-oriented programming) but because we are reusing gamestate for our other games in assignments 2 and 3, we created gamestate as generally as possible so that it would work for any two player, zero-sum, full knowledge game.
There are some differences in methods in OOP and functions, some of which was covered already. They are fundamentally different in that methods are always in a class and defined by that class while functions are not. Methods can be written in the parent class or child class. Three other important special methods we have used frequently so far are the __str__, __repr__, and __eq__ methods. These are used to represent data in forms readable by users, for python, and for equating instances.
In this course, inheritance has so far mainly been dealing with subclasses. Inheritance is basically how subclasses/child classes derive data and behavior from its super class/parent class. To inherit the attributes of a parent class, we would write, for example, Square(Shape). Subclass methods can also override methods of their parent class if the parent class method is implemented to raise an error stating that the method should be implemented in the subclass. We used this frequently in our first assignment and will be using it in our second. We could have coded subtract square without using subclasses (we could even have coded it without using object-oriented programming) but because we are reusing gamestate for our other games in assignments 2 and 3, we created gamestate as generally as possible so that it would work for any two player, zero-sum, full knowledge game.
There are some differences in methods in OOP and functions, some of which was covered already. They are fundamentally different in that methods are always in a class and defined by that class while functions are not. Methods can be written in the parent class or child class. Three other important special methods we have used frequently so far are the __str__, __repr__, and __eq__ methods. These are used to represent data in forms readable by users, for python, and for equating instances.
No comments:
Post a Comment