Classes are the workhorses of object-oriented programming languages (OOP) like Python. As we saw, everything in Python is an object. Strings, lists, functions and even modules are objects and, as objects, they all have attributes and functions (which in the case of objects are called methods) associated with them. Let’s take the example of a list:
myList = [1 ,3.4, 5, 66, 298]
myList.append(243)
myList.sort( )
In the above examples, append() and sort() are methods of the object list, and can be called by using the construction objectName.method(arguments).
Lists, integers, strings, etc, are built-in objects in Python. But you can create your own objects by using classes. Classes are object definitions created by the user, and work as a place to collect functions and attributes related to the object you want to create.
When you put functions and variables into a class, they have a way to “talk” to each other and keep information together. Here is an better explanation and example taken from a tutorial on the web:
For example, imagine you have a golf club. It has information about it (i.e. variables) like the length of the shaft, the material of the grip, and the material of the head. It also has functions associated with it, like the function of swinging your golf club, or the function of breaking it in pure frustration. For those functions, you need to know the variables of the shaft length, head material, etc. (…)
What happens if each time you use your golf club, the shaft gets weaker, the grip on the handle wears away a little, you get that little more frustrated, and a new scratch is formed on the head of the club? A function cannot [handle] that. A function only makes one output, not four or five, or five hundred. What is needed is a way to group functions and variables that are closely related into one place so that they can interact with each other.
Chances are that you also have more than one golf club. Without classes, you need to write a whole heap of code for each different golf club. This is a pain, seeing that all clubs share common features, it is just that some have changed properties - like what the shaft is made of, and it's weight. The ideal situation would be to have a design of your basic golf club. Each time you create a new club, simply specify its attributes - the length of its shaft, its weight, etc. (…)
These problems that a thing called object-oriented-programming solves. It puts functions and variables together in a way that they can see each other and work together, be replicated, and altered as needed, and not when unneeded. And we use a thing called a 'class' to do this.
To use classes, the same way when you create functions, you have first to define them (class definition), and then call them by creating instances of this class. Like what we did in class:
#### CLASSES
#defining a class
class Student:
#first define the class constructor
#which is the function that is executed
#every time you create an instance of this class
def __init__(self, name, attendance):
#in this function, we pass three arguments:
#self should ALWAYS be passed as the first argument
#in every function inside of a class definition
#the following arguments are defined by you
#depending on what you want to pass for the instance
#when you are creating it
self.name = name
self.attendance = attendance
self.marks = []
#in the lines above we declare a series of variables
#inhrent to the class, which will later work as
#attributes of each instance of the class you create
#as use of the "self.nameOfVariable" syntax indicates
numberOfAssignments = 0
#above we created a local variable which will exist only
#inside the class, and cannot be accessed as attributes
#of the instances you create (note we don't use "self"
print "New student was created"
print "this student did", numberOfAssignments, " assignments"
#the lines above are simple print statements which will
#show when you create an instance of the class,
#because they are located on the __init__ function
#now we leave the __init__ function and we can than create
#as many internal functions as we want
#(which are called "methods" of this class
#note that all of them MUST take as a first argument
#the variable self, so that they always refer to the
#instance itself
def addMark(self, mark):
#this is a simple function which
#appends whatever value we pass as an argument
#to the internal list "marks", which was
#created in the __init__ function
self.marks.append(mark)
def addAttendance(self):
#the same with this method, which doesn't take
#any additional arguments, but still makes modifications
#(by adding one unit) to the attendance attribute of the class
self.attendance += 1
Now that we have our class define, we can create instances of it and call its attributes and functions easily:
## creating an instance of the class
myStudent = Student("Liu", 10)
#as you see, we create instances of a class
#by simply assigning it to a variable and
#passing the necessary intial arguments
#required by the __init__ function
#Note that we don't ever need to pass "self" as an argument
#on the instance creation, as Python automatically
#does it internally
#now we can access the attribute of these functions, also
#defined on the __init__ function, by using a simple construction:
print myStudent.name
print myStudent.attendance
#Here lies one of the powers of classes: I can create
#as many instances of that class as I need,
#and all of them inherit the methods and attributes of the class
myStudent2 = Student("Grisha", 20)
print myStudent2.name
print myStudent2.marks
#the same way as I accessed attributes aobve, I can
#call the class's methods:
myStudent2.addMark(10)
print myStudent2.marks
This was a short and small example of classes. You can do many powerful things with it as we will see next class. So keep in mind that the class definition works as a blueprint, from which as many instances as you want can be generated.
No comments:
Post a Comment