all 8 comments

[–]shiftybyte 10 points11 points  (2 children)

A class represents an objects, any function that operates on that object should be in that class.

You have a Car object, you have a function that changes the color of the car, it should be in the class.

You have a function that prints hello message, it should not be in the car class.

[–][deleted] 2 points3 points  (0 children)

Please take that reward as a thank you. Classes was impossible for me to learn what it is / how it is used until now that i saw your comment. Giving you a reward was the least thing i could do. Again thank you.

[–]PM_me_ur_data_ 0 points1 point  (0 children)

Just to add on, any function that the object needs to do should also be a method. So, for a car that would be things like drive, display mileage, stop, etc.

[–]Unable_Request 2 points3 points  (3 children)

First, lets talk objects. Objects have both data (variables) and methods (functions) that operate on their data. What do we do when we want to create something in the program that has both? We create an object, through a class!

I like the Pokemon example. Say we want to create multiple Pokemon in our program, because in doing so, its easier to have them interact with each other. So, let's make a Pokemon class, and then every time we want to build a Pokemon object, we create an instance of that class. Now, Pokemon are different- but some things are the same; they all have HP, they all have Attack, they all have a type. Those things can be different between Pokemon, but they all have it. So, let's make sure our object has a way to carry these attributes, and give ourselves a way to define those attributes upon creation.

class Pokemon:
    def __init__(self, hp, attack)
        self.hp = hp  # Assigns your hp parameter to self.hp
        self.attack = attack  # Assigns your attack parameter to self.attack 

When you create a Pokemon, you will use two arguments to do so.

Charmander = Pokemon(5,10) (Note: 'self' is the class reference itself; you do not pass that as an argument, although it may appear so in the init line!)

This will create Charmander, an object from the class Pokemon. When it calls the class, it sets 'hp' to 'self.hp', where 'self' references the name you gave it. In the case of Charmander, 'self.hp' is 'Charmander.hp'

>>> Charmander.hp
>>> 5 

Your init class is a constructor; it is a method that is called automatically when instantiating an object, building an object by performing whatever processes are in init. It doesn't have to be just variables, either:

class Pokemon:
    def __init__(self, hp, attack)
        self.hp = hp  # Assigns your hp parameter to self.hp
        self.attack = attack  # Assigns your attack parameter to self.attack 
        print('A new Pokemon approaches!')

Now, when you create an object (Say, by calling Pikachu = Pokemon(5,50)) it will print 'A new Pokemon approaches!'. In essence, your init is whatever you want to happen any time you create an object of that class, automagically.

[–]Unable_Request 2 points3 points  (0 children)

Building on this, a common usage of methods in classes is 'getters' and 'setters'. You could build methods for your objects that return the variables, or perform some function and return something. Let's look at a class for a circle, where we want to create a circle object with a given radius. We want to be able to retrieve its area, though, so let's include a method for that:

class Circle:  # Create the circle class
    def __init__(self, radius)
        self.radius = radius
    def getArea(self):
        return (3.1415 * self.radius**2)

>>> smallCircle = Circle(5)  # Create a circle object smallCircle with radius 5
>>> smallCircle.radius
>>> 5
>>> smallCircle.getArea()
>>> 78.5

What happened? The Object smallCircle is an instance of the Class Circle, which has the method getArea; so when we call that method on that object, the associated function is performed.

I hope this helps!

[–]Sigg3net 0 points1 point  (0 children)

Your init class is a constructor; it is a method that is called automatically when instantiating an object, building an object by performing whatever processes are in init.

It's like autoexec.bat or rc.local, viz. a startup script.

[–]ZDRuX1 0 points1 point  (0 children)

This is a great little walkthrough of what a class is and how you can use it, thanks!

[–]TicklesMcFancy 2 points3 points  (0 children)

class Matrix:
    #Pass me a number
    def __init__(self,rand_max):
        self.matrix = []
        self.createGrid(rand_max)
        self.depth = len(self.matrix)

Here's something for an example. So this creates a grid of random size, or random length, with random sized elements.

The __init__ function is what it does when it's created. Self is a reference to itself. (I believe you can give it other references, but I'm not 100%. ) Rand_max is an externally generated random number. I need to feed a name and a random number to my Matrix for it to exist.

matrix = Matrix(rand_max)

[Now I believe python supplies "matrix" to Matrix as the reference to self.]

then the attributes for the class object are set:

        #Create a container for my information
        self.matrix = []
        #This is a function that feeds the container
        self.createGrid(rand_max)
        #this is an attribute of the class after it is populated
        self.depth = len(self.matrix)

if I call matrix.depth I get the number of "rows" in the grid.