Creating a Class

Ive looked up the code for how to create a class in python but I dont quite understand some parts of it. Here are two differrent examples I found:
######################
class ClassName:
<statement-1>
.
.
.
<statement-N>
######################
class Employee:
‘Common base class for all employees’
empCount = 0
def init(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1

def displayCount(self):
print “Total Employee %d” % Employee.empCount
def displayEmployee(self):
print "Name : ", self.name, ", Salary: ", self.salary
########################

My questions are:

  1. in example 1, what does <statement-N> mean? is it just part of the example and represents any statement?
  2. in the second example, what does “def init(self, name, salary):” mean? is it necessary for the creation of the class?
  1. Yes. ‘N’ represents the index of the last in a series of statements you will have in a class, which is also the count of all statements. It’s just a way to explain things.
  2. It’s not really clear to me what the script does because you didn’t display indentations. Maybe you could wrap code tags around the original code (with indentations)? Go to advanced, top right.

The “def init” line defines a new method (function) in the class. It is not required but it is often recommended. The init method is a special method that is called automatically when you make a new instance of the class. You can do one-time setup code here or provide ease-of-use methods.

For example. If you are going to make an Employee object, it doesn’t make much sense if they don’t have a name and salary assigned. So instead of something like this:


emp = Employee() 

# If somebody forgets to fill these in then the object is useless.
emp.name = "test"
emp.salary = 100.0

With the init method defined you can do this:


emp = Employee("test", 100.0)

That way there is no way that the object has been created with meaningless values.

I wrote a brief example of what a class is, and how to use it the other day. If it helps, here it is:

Classes are like rocket ships. They are similar, but all different. Let’s have a look at this class:
Code:

class rocketShip:
    def runEngine(self):
        self.fuel -= self.engines
    engines = None
    wings = None
    fuel = 0

Now we make some rocket ships:

rocket1 = rocketShip()
rocket2 = rocketShip()

rocket1.engines = 2
rocket1.wings = 0     #Setting stats for rocket 1
rocket1.fuel = 30

rocket2.engines = 1
rocket2.wings = 4    #Setting stats for rocket 2
rocket2.fuel = 5

rocket1.runEngine()  #Running the rockets
rocket2.runEngine()

print(rocket1.fuel, rocket2.fuel)

I suggest to work through a basic Python lesson especially the OOP (object orientated programming) part which is often labeled “Objects and Classes”. Such lessons usually contain a lot of explanations and examples that a beginner can understand whats going on and how to use them. This is not BGE specific.

I understand. However, it seems to me that the majority of my python questions are answered in the game engine section and not in the python section…lol

ok. so basically, the “init” method, which is specific to Classes, just creates and sets the data for the newly created object from the class? But another affective way of doing the same thing is to just define the data in the class as if I were setting properties for currently existing objects? And for inside of the parenthesis for “def init(self, example, example):”, the first word “self” refers to the class. And the second and third etc refer to the variables below that are to be set?

When I tell you this is not true, will you go for a Python book?

I suggest Byte Of Python

pretty complex stuff .

better learn how make a custom function (this is a great tool and much more easy to learn).

to be honest i not understand what class give more than function…if is just a syntax or there other…

Classes can be used to store data, operate on itself. It’s like a package. It can be used for creating abstracted interfaces, data types. It is not essential to program with classes but because everything in python is a class you may as well learn about them

Sent from my Nexus 7 using Tapatalk HD

if fact , i want learn anyway :slight_smile:
the problem is that not there many example bge-related.

Oop and therfore Classes are not bge related. This means whatever book you read you can use this knowledge in BGE or any other oop software. You need a basic understanding what a class and an object is and how they differ. You need an understanding of the principles of OOP which is language independent. You can read them in wikipedia and ALL books have examples to demonstrate oop in one form or another. You do not need the bge to define a class. You simply need Python and an text editor.

If you want more guidance have a look at http://www.pyschools.com/. It comes with exercises too.