Re: Python crash course

classes

A class is like a template. You say "I want to make a cat" and you create a cat class:

class Cat(object):
    pass


Now you have a class for a cat. Next we need to figure out what a cat has and what it does. A cat has an age, so we can identify an age. It also has a name, so we can name it. It also purrs and it catches mice.
So lets create this template for a class with age, name, purr and catch mice.
We are going to use something called an init function. init is short for initialize and is run every time you create an object. We can pass the init function arguments, so we will do that. We can identify the attributes of the cat class by using the "self" keyword. So in the init function we pass in some variables as arguments and we set the self.variables to equal the passed in variables.
Cat class:

class Cat(object):
    def __init__(self, name, age):
      &n bsp; self.name = name
        self.age = age

    def purr(self):
        print("purr... purr...")

    def catch_mice(self):
        print("%s goes and stocks a hapless mouse and at the last moment the cat pounces on it! Yummm..." % self.name)


OK, so now we have a cat class with a self.age and a self.name. This Cat class is not a cat, it is all cats, so we need to create a cat that we can pet and play with:

midnight = Cat("midnight", 3)

Now we have a cat named midnight. Lets look at his age:

print(midnight.age)
3

This means that when we were creating the Cat class "self" just identified this nebulous, unidentified object. We needed to actually "create" a cat by saying "I am god, let there be midnight that is a Cat!" Now if you needed to change something to only midnight, you woul d need to use midnight rather than self. Otherwise you would change all cats.
For example:

midnight.age = 5

print(midnight.age)
5

So we have midnight who is an object of Cat. What else can he do? He can purr! Lets watch him purr:

midnight.purr()
purr... purr...

Awesome! Now your midnight cat purrs and has an age. He also has a catch_mice function he can do as well! But what happens if we try and make our midnight bark? We will get an error that midnight doesn't have that function. We can make it though:

def bark():
    print("Midnight expands his neck into a veritably massive lump of flesh and lets out the strangest woof ever!")

midnight.bark = bark

So now we have, in a sense, trained our midnight to bark. Lets say we would like another cat, lets create one:

frisky = Cat("Frisky", 7)

Now we have 2 cats, frisky and midnight. Lets check that they are both cats:

prin t(frisky.age)
7

print(midnight.age)
5

frisky.purr()
purr... purr...

midnight.purr()
purr... purr...

midnight.bark()
Midnight expands his neck into a veritably massive lump of flesh and lets out the strangest woof ever!

frisky.bark()
ERROR ERROR!!!! Cats can't bark!

Frisky has all the properties of a cat, but not the special property we gave to midnight. we would have to give him special training in order to have him bark.

This is a class. It is really simple, so sorry for taking such a long post to explain it. A class is just a template, like a document or email. You create the class which has fields and properties that you specify (an email has subject, to and body for the properties and send, reply and forward for the functions). Then you create an instance of that object. midnight = Cat("Midnight", 3). When we create the object, our __init__ function is run, setting the self.name as name and th e self.age as age. Then it replaces self with midnight.
That's all, when you create midnight, you are saving that specific template under the name of midnight. It is an instance of the template Cat, but is it THE template Cat? no! midnight is just 1 Cat.

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : momo7807 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dhruv via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dhruv via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : frastlin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : dhruv via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector

Reply via email to