Objects and Classes
Object-oriented programming. Although you don't need to understand objects to make interesting programs, objects can help you make interesting programs a lot quicker and with a lot more readability. Instead of designing super complicated logical arguments in our programs, we use something called 'classes'.
I think Codecademy does a great job introducing the concept of classes in its Python Course. If you haven't already tried it, take a couple of hours and check out this lesson on classes. Be sure to log in to save your progress!
Sometimes going through the material a different way is helpful. You can check out http://greenteapress.com/thinkpython/html/thinkpython016.html for a different approach to classes. I suggest creating a module instead of using the interpreter.
Quick Review of Classes:
1) We make specific objects based off a class. A 'class' is like a generic person--kind of like a cookie cutter. You can say the person has some characteristics: gender, age, weight, IQ, ability to tell jokes, etc. We can then use the generic person class to create specific people, we call these instances of the class.
#here we define our generic person class.
class Generic_Person(object):
#all classes start with an __init__ function (two underscores before and after)
#that takes "self" as an argument (or parameter) and defines the attributes of the class.
def __init__ (self):
self.gender = ' '
self.tells_good_jokes = "True"
#here we make and instance of our class named "Andrew".
andrew = Generic_Person()
#we can give our person attributes. We use the form: "class.attribute = "
andrew.gender = 'male'
#we can use inheritance to see if our instance, Andrew, tells good jokes
>>>andrew.tells_good_jokes
True
#here we define our generic person class.
class Generic_Person(object):
#all classes start with an __init__ function (two underscores before and after)
#that takes "self" as an argument (or parameter) and defines the attributes of the class.
def __init__ (self):
self.gender = ' '
self.tells_good_jokes = "True"
#here we make and instance of our class named "Andrew".
andrew = Generic_Person()
#we can give our person attributes. We use the form: "class.attribute = "
andrew.gender = 'male'
#we can use inheritance to see if our instance, Andrew, tells good jokes
>>>andrew.tells_good_jokes
True
2) Functions defined inside a class are called "methods".
class.method(argument)
class.method(argument)
3) We can have objects inherit the properties of other objects.
#this is the parent class
class Parent_class(object):
def __init__(self):
pass
#this child class inherits all the characteristics of the parent class. Notice the argument is the parent class name.
class Child_class(Parent_class):
def __init__(self):
pass
#this child class inherits all the characteristics of both the above parent classes. Notice the argument is the parent class name.
class Grandchild_class(Child_class):
def __init__(self):
pass
#this is the parent class
class Parent_class(object):
def __init__(self):
pass
#this child class inherits all the characteristics of the parent class. Notice the argument is the parent class name.
class Child_class(Parent_class):
def __init__(self):
pass
#this child class inherits all the characteristics of both the above parent classes. Notice the argument is the parent class name.
class Grandchild_class(Child_class):
def __init__(self):
pass
A More Extensive Review of Classes
If you're looking for more in the way of review, particularly regarding functions, methods, and inheritance. Here's where to go.
Let's try it:
As a practice program, we are going to create a hierarchy program which highlights the inheritance of classes. When creating a program, it is often helpful to spend some time thinking about the structure BEFORE you start coding. What are the relationships between the objects? What will the flow look like?
|
1) Quadrilaterals.
We'll do some games once we talk about tkinter together.
|