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.

Monday, June 3, 2013

[Basic] How To Create Classes In Python [ Object Oriented Programming ]



Let's start by explaining one thing at a time...


Classes
in python, are a way to bundle data [information that is fed to the code, or that you 'hard-code' yourself], with associated code, making it easier to manage the code itself and fix bugs if they ever arise. They can help by allowing you to model the problem you’re trying to solve and manage a program’s complexity better.


Let's say you're creating a fighting game where there are two types of NPC's. Bystanders, and fighters. You'd create the "bystander" class as easily as this:

class Bystander:
    pass


You've created the Bystander class. Why is this so great? Because this means that you can set this class to as many variables as you want, and you'll be able to have numerous different "versions" or "instances" of each:

bobby = Bystander()

sarah = Bystander()

"bobby" and "sarah" Are two different "instances" of the same class called "Bystander".

They are what are known as objects. Individual "occurances" or "copies" of the same class. You'll understand why this is so cool in a minute.

Let's modify our class a bit to get a better grasp of what we're dealing with here.

# We created the class called "bystander".
class Bystander:
# Here, we'll have a docstring. They're good for telling what
# our classes do.
"""A Simple Bystander class. These Individuals all have their own greeting."""
    # Now we create what's known as a "constructor" [__init__]
    # Which allows us to take the variables that are passed to the
    # class when the object is "instantiated" or "created".
    # It uses self because it is actually referring to the variable
    # that you use to create the objects.
    def __init__(self, name, greeting = ""):
        self.name = name
        self.greeting = greeting

 #Then we instantiate our objects.
bobby = Bystander("Bobby", "Hiya!")

sarah = Bystander("Sarah", "Konnichi wa!")
 # Notice that Alex doesn't have a greeting, but there will not be an error.
 # That's because the greeting is a "kwarg" or a "keyword argument". It
 # Is an optional argument or "parameter" that can be passed.
alex = Bystander("Alex")
# Please note that no matter how many objects we have, we'll always
# know what objects of this specific class are capable of in the case of 'Methods'.

A Method is a function of a class that is made specifically to be associated with the class itself. Methods must always take the "self" parameter because when it uses "self" it is actually referring to the object itself.


class Bystander:
""" A Simple Bystander class. These individuals have their own greeting."""

def __init__(self, name, greeting = ""):
self.name = name
self.greeting = greeting 

def say_Name(self):
print(self.greeting + " My name is", self.name + ".") 

bobby = Bystander("Bobby", "Hiya!")

bobby.say_Name()

Kira = Bystander("Kira", "Oh, yeah, baby!")

Kira.say_name()
Running this through the interpretor, we should get:

Hiya! My name is Bobby.
Oh yeah, baby! My name is Kira.

I hope this should provide a basic enough insight on how classes and objects are made. I'll leave the rest up to your imagination.

2 comments:

Anonymous said...

First of all I want to say excellent blog! I had
a quick question which I'd like to ask if you do not mind. I was curious to know how you center yourself and clear your mind before writing. I've
had trouble clearing my thoughts in getting my thoughts out.
I do enjoy writing but it just seems like the first 10 to 15 minutes are lost just trying
to figure out how to begin. Any ideas or tips? Thank you!


Look at my web page ... reklama level 5

Unknown said...

First, I'd like to say, thank you for commenting! To answer your question: when I write, I always try to draft what I'm writing before I put it up. Sometimes, I rush what I've written and I go back and revise it, but only in the case where it is something irrelevant, or a multimedia issue. Occassionally, I make mistakes, and that's normal, I take criticism very well, actually, as long as it's constructive and not demeaning or disrespectful.