image
image
image

Chapter 3: Working with Classes and Objects

image

Objects and classes are important when it comes to working in the Python language. The objects help to define the different parts of the code and keep them all organized and easy to understand while the classes are going to work as the containers for the objects so that objects that are similar to each other to help the code work better.

One thing that you should remember when working with objects is that when they are grouped together in a class, you can make them anything that you want. But they should have some kind of similarity to each other if they are in the same class. This helps to keep things in order and will help others to be able to understand your class. It is just like organizing your closet during Spring; you would put the shoes in one place, the clothes in another, the purses in a third, and so on. The classes that you create can have any of the objects that you want inside, but they should make sense to be grouped together to help your program work properly.

So basically, the objects are going to be the various parts that are inside the code, and the classes are going to be the containers that will hold on to the objects you have created and which are similar to each other in some manner. You should be careful when labeling each of these because you want the objects and the classes, or their storage containers, to work well together and actually make sense for what is inside. You don’t need to put in parts that are identical to each other, but make them similar in a way that if others were looking at them, they would understand the process that goes along with it.

Classes and objects are a great way to learn more about programming and can keep all your information as organized as possible. As long as you are able to create the classes in the proper way and keep the right objects inside of it, you are sure to see the results.

How do I create a new class?

So the first thing we should look at is how to create a class inside of Python and luckily this is a pretty simple process to work. When working on a statement for a class, you must also take the time to create a new definition. You should place the name of the class right after your keyword and then your superclass will be inside the parenthesis. And don’t forget to add in the semicolon, which isn’t necessarily required because the code will still be read by the compiler, but it is considered good programming practice to add it in. Here is an example to show how you are able to create your new class when working in Python:

class Vehicle(object):

#constructor

def_init_(self, steering, wheels, clutch, breaks, gears):

self._steering = steering

self._wheels = wheels

self._clutch = clutch

self._breaks =breaks

self._gears  = gears

#destructor

def_del_(self):

print(“This is destructor....”)

#member functions or methods

def Display_Vehicle(self):

print(‘Steering:’ , self._steering)

print(‘Wheels:’, self._wheels)

print(‘Clutch:’, self._clutch)

print(‘Breaks:’, self._breaks)

print(‘Gears:’, self._gears)

#instantiate a vehicle option

myGenericVehicle = Vehicle(‘Power Steering’, 4, ‘Super Clutch’, ‘Disk Breaks’, 5)

myGenericVehicle.Display_Vehicle()

Now take some time to write this out in your compiler. As you are writing it out, you may notice that there are many parts inside that you have to keep track of. First it has the definition of the object, the definition of the method, the attributes of the code, and the destructor function. There are also the regular function as well as the class function as well. To understand why all of these are important, let’s talk about each of the parts and what they mean.

Class definition

You will need the class definition and the object instantiation as part of the class syntax. These help tell your compiler what is going on and gives the commands that are needed. Any time that you would like to invoke the class definition inside of your code, you would just need to add the object.method() function or the object.attribute to help get this done.

Special attributes to add into the code

There are some special attributes that are recognized just inside of the Python code. It is a good idea to learn what these are all about because they help to make it easier to work on any code that you want. It is also nice to have the peace of mind knowing that the interpreter will see these attributes and will know how to use them inside the code. Some of the attributes that are important when working in Python include:

__bases__: this is considered a tuple that contains any of the superclasses

__module__: this is where you are going to find the name of the module and it will also hold your classes.

__name__: this will hold on to the class name.

__doc__: this is where you are going to find the reference string inside the document for your class.

__dict__: this is going to be the variable for the dict. inside the class name.

Accessing the members of your class

There are a few different options that you are able to use when it comes to accessing the members that are inside the classes that you are using. And while all of them are going to work, going with the accessor method is seen as the best because it allows you to encapsulate, or provide the information, right inside the syntax to make things easier and to ensure that you are able to read the code easy later on. A good example of how this works includes:

class Cat(object)

itsAge=None

itsWeight=None

itsName=None

#set accessor function use to assign values to the fields or member vars

def setItsAge(self, itsAge):

self.itsAge=itsAge

def setItsWeight(self, itsWeight):

self.itsWeight=itsWeight

def setItsName(self, itsName):

self.itsName=itsName

#get accessor function use to return the values from a field

def getItsAge(self):

return self.itsAge

def getItsWeight(self):

return self.itsWeight

def getItsName(self):

return self.itsName

objFrisky=Cat()

objFrisky.setItsAge(5)

objFrisky.setItsWeight(10)

objFrisky.setItsName(“Frisky”)

print(“Cats Name is:”, objFrisky.getItsname())

print(“Its age is:”, objFrisky.getItsAge())

print(“Its weight is:”, objFrisky.getItsName())

You would get an output from this syntax that states the name of the cat is Frisky, the age is 5 and the weight is 10 based on the information that we put in. Remember that you are always able to change the information that you put in to get the results that you would like.

Working in classes is a great way to help keep all your information sorted together and making sense. You need to place the objects inside, making sure that they share some kind of similarity to each other to keep the classes as organized as possible. With some of the codes provided in this chapter, you will be able to write out how to create some of these classes and get the results that you would like.