Just A Small Guideline

Feel free to browse the site. Don't be afraid to leave comments, they're supportive.
Please follow me on Twitter @ehldmnt and retweet my posts.

Wednesday, June 12, 2013

Python 3: Classes; a quick examination.

Object Oriented Programming [OOP] is not really a new concept. It's something that Python does, and does well [even if there is room for improvement]. This guide goes as far to explain pretty much how classes work in Python. Comment below with any questions or comments.

I feel that there really aren't many tutorials out there to explain this a bit more in depth. Here is a much more well rounded explanation [compared to the sparse material I've found]:

Classes are basically what they're called, "classes" or "classifications" for certain "kinds" of things. We can take for instance a chair, and a dog. They both have four legs, but which one is made of wood? And what exactly is a leg? Something that can move or something that holds the chair up? We could create a class called "animate".

class Animate:
pass

Well, isn't that nice? We have a class that does absolutely nothing. But that's okay, it'll all fall into place. The pass statement allows us to create something that syntactically requires some code or data, but doesn't actually do anything at all. Let's create a second class called "Inanimate".

class Inanimate:
pass

Once again, we have a class that does nothing, but the diference between the two of them is their namespace. We now have an "Animate" and an "Inanimate" class. Now for objects.

chair = Inanimate()

dog = Animate()

This seems like old news, "We're just renaming the classes to chair and dog!". Not quite. In OOP [Object Oriented Programming] we can have as many inanimate or animate objects as we'd like.

pencil = Inanimate()

cat = Animate()

 Are two other seperate "instances" of the class. In fact, this is called "instantiating." In this case, for those of us who don't quite understand, a dog is an "instance" or a particular "existence" while a "cat" is a separate instance. To learn more about what can be done, let's edit our classes directly.

class Inanimate:
def __init__(self, name, type):
self.name = name
self.type = type

We've done a lot here, so let's go over it piece by piece. Each class has a special "function" called a constructor. The __init__ function. It allows us to specify attributes of each particular class that we have. It makes us creating different classes a lot more structured. Inside of the __init__ constructor, we see one element called "self" and we see two elements called "name" and "type". If they were specified like this:

def __init__(self, name, type = "Furniture"):

Then it would make all inanimate objects "Furniture" by default, but it would also make the particular attribute "type" optional, like optional arguments on a command line, which is both a good and a bad thing. If you made it "item" or even just an empty string, that would be far more practical. Because in that case, that would mean that "Pencil" would be furniture, too. The reason why you set self.name to the "name" parameter, is because you're basically setting the dot operator, as well as making whatever you put in the class at instantiation permanent to that specific instance of the class.

Pro Tip:

You can make each of the variables private by adding one or two underscores (_) to the beginning of each attribute like this:
def __init__(self, name, type:)
self._name = name
self._type = type

This actually much more use, like for example, if you didn't want people to alter your class, or when they didn't need to alter the class at all, using the API to work with it.

"But what is self?!" Self is referring to the "variable" of the object itself. When the python interpretor runs, it goes like this:

- It sees the class you made.
- It sees that you instantiated [Remember, that means 'create'!] an object.
- It takes the name of that object and goes back to read the class. It replaces self with the title of the object you had given it.
- It continues running the script.
 
Now we must edit our "chair" object.

chair = Inanimate("Chair", "Furniture")

dog = Animate()

Hm... That's nice, but let's say you wanted to return self.name? How would you go about doing that? Why, with a method of course! Methods are just functions that exist inside of a class that can be used along with flow controls [if, for, while] in order to increase the functionality. Let's create a method that returns the name of the item!

class Inanimate:
def __init__(self, name, type):
self.name = name
self.type = type

def getName(self):
return self.name

chair = Inanimate("Chair", "Furniture")

print(chair.getName())

Using this, we can actually reduce the amount of code we write later. Because we're using classes, we can do this for EVERY class. And that is some of the power of Object Oriented Programming.

No comments: