Python Programming 9

Wait. What? You've made it this far into programming? That's not supposed to happen! You will rue the day you enjoyed code.

Welcome to object oriented programming.

What is object oriented programming? Basically it's a concept made up in the early days of programming. It was then hyped to death by uber tech companies in the 1980's (seriosuly, we had these idiots running the show). And since then has remained a key time to weed out programmers from drones... to of course dispose of the programmers.

(okay one more pirates video)

Ok, maybe I'm very biased. But so are other people. I happen to believe in functional programming when I do it and OOP when I use it. You'll learn very quickly that having strong opinions is not just for politics and english literature. The internet is packed with forums of software engineers yelling at each other over which way to code is the way.

We're going to learn an evil but necessary way to program called OOP. It's not all bad. Many of these concepts are super important for translating knowledge across languages. And Python classes (OOP) are probably the second easiest I've ever had to learn. Let's jump into an example.

class MyClass:

____variable = "blah"

____def function(self):

||||||||print "This is a message inside the class."

First we create a class by calling the class keyword and making what looks like a variable. Inside the class we can create a variable and a function. The function inside a class always takes a self parameter. The reason why is so we can refer to the class inside the function. That probably has lost you, and that's totally okay.

Let's create a new object:

newObject = MyClass()

newObject.variable

print newObject.variable

This will print 'blah'. This is because we create a new object based on our 'MyClass' class. This new object takes a variable, which since is not defined becomes the default of our class 'blah'. Then we are simple printing this variable out. It may not look like anything, but we are objects which can be used in the future to describe complex ideas. We are writing quite a bit of code to do this, however. You must be prepared for the amount of work you need when creating classes to create objects. This is where the expression, 'you wanted a banana but you got the gorilla and the jungle too', comes from. We can keep creating new objects with the same class too.

anotherObject = MyClass()

anotherObject.variable = "Changing it up"

print anotherObject.variable

This will print "changing it up" because we redefined the default variable in our new object.

anotherObject.numbers = [1,2,3,4,5,6]

print anotherObject.numbers

We can also create new variables and attach them to this object. We attached a variable called numbers on to the end of our object and set it equal to a list of numbers. We can print this list out.

print anotherObject.function()

Remember when our class had a function? We can call that default function on our new object. This in case you forgot will print out 'This is a message inside the class'. The whole idea behind OOP is that you should develop a class which represents an object.

Imagine a dog. We are aware that there are many different kinds of dogs. Golden retrievers, german shephards, poodles, uh the ones that are brown and white... the taco bell dogs, lassys and old yellers. So what we can do is generalize what all dogs have in common to make a default class of a dog. Then when we want to make a single kind of dog, we have most of the basic attributes down and can redefine as necessary.

class Dogs:

____voice = "bark bark!"

____num_of_legs = 4

____tails = 1

____desires = ['play outside', 'eat meat', 'catch ball', 'be pet']

So we created a class called Dogs which has some basic attributes of your typical dog. A typical dog says 'bark'. They tend to have 4 legs. They tend to have 1 tail. They usually have the desires listed. Now that we have a general idea of what a dog is all about, lets create a new dog based on the basic attributes.

blackLab = Dogs()

print blackLab.num_of_legs

print blackLab.desires[3]

Here we made a blackLab dog and gave it the basic dog attributes. Then we simply called and printed those attributes. This will work as expected.

blackLab.color = "Black"

print blackLab.color

Here we created a new variable for the blackLab object called color. After setting the color to be black, we can use it to do what we want. We simply print it out here.

So where would you use stuff like this. Wherever you have large entity sized projects, you typically want to create classes so that making objects later on saves lots of time. Heres a good real world example. Bungie, the company that created the Halo series, had to develop classes to help foster character creation.

Here you see spartan creation in Halo 3 (2007). Basically, what Bungie (probably) did was create a class filled with attributes regarding your basic spartan (master chief look alike). Then the player could go in and customize everything from color to armor pieces by simply making a new object off the main spartan class and editing attributes as needed.

A lot of these concepts might seem foreign to you. And maybe you just don't see how this could be applied yet. That's okay. This is why OOP is one of the most difficult concepts to grasp for programmers (also one of the reasons so many choose functional programming instead).


Topics: Jedi, Athlete, College

Write a class for one of the 3 topics. Give the class 5-8 default attributes and then make 4-5 objects from that class. Use the objects to print off information or some other task.

results matching ""

    No results matching ""