This video demonstrates how to create your own python class and then use it from the main program. The video demonstrates this process using the Spyder IDE. However, you can use any Integrated Developer Environment or IDE for this purpose. The Spyder IDE comes bundled along with the Python XY distribution and if you are using Spyder, you can launch and create a new project. For demonstration purposes, the project in the video demonstration is named as “working_with_objects”
To create a module, you can right click on the project, select new and chose module. You will be prompted to name the module in the next pop up. For demonstration purposes, the module is called as “automobile.” This will create a new module and once a module has been created, you can create a class inside it. You can name your class anything you like. There is no rule that suggests you need to name the class same as the module you have created. However, in this video, the class is also named as automobile. You could even define multiple classes inside a single module.
When you are creating a class inside a module, all you will be doing is creating your own data type. The data type will include some attributes, methods, and some functions associated with it. A class is defined as follows “class Automobile(object);”. What you see inside the parenthesis here the word “object” is something like a superclass for the class “Automobile.” When you associate the superclass ‘object’ with the class ‘Automobile’ the class ‘Automobile’ assumes all the attributes and methods that are associated with the class ‘object’.
You could even add a constructor for your class. A constructor is nothing but a function. It is sometimes also called “double underscore” and also “init double underscore”. The way you define a constructor is as follows, “def__init__(vin, make, model)”. The def in example is nothing but a keyword that is used to define a function. The parenthesis after the init are used to define any attributes that you need to associate with the constructor function.
When you define any attributes, you also need to define the default values for each of these attributes. If you give the default value of any attribute as an empty string like “model-‘ ‘” this will make the attribute optional. Once you do that, you also need to add the word ‘self’ before each attribute. This will make sure that each of these attributes refer to itself when instantiated. Make sure your constructor is in this format def__init__(self, vin, make, model=’ ‘);. Next, you need to define the objects in this manner “self.vin=vin. You could even have the local variable defined within the constructor to say ‘v’ then the object definition becomes “self.vin=v”. Next you will need to repeat that for each of the attributes. Once you have done this, you can define even more functions with these attributes but it is a good idea at this point of time to test your program.
Testing your program
The best way you can test your program would be to instantiate any variable. For example, you could say a = Automobile ( ‘14567328’ , ‘Toyota’ , ‘Prius’); Once you have done this, you can simply print out different attributes using what is known as dot notation. For example “print a.make, a.model. The next thing you need to check is what happens if you simply enter “print a”. Once you have done both of these go to the python interpreter and run your code and take a look at the output displayed on the right bottom side of the screen. What you will notice along with the output is some hashcode which you need to eliminate.
The hashcode appears whenever your print your function without having a compensating string function. So it is always a good idea to provide a string function to compensate for the undesired output. The string function that you need to use to remove this hashcode begins and ends with double underscore and it looks like this “ def__str__(self); Notice that the first argument is “self” again. After this you need to add a return statement that will return a string which looks like this “ return ‘vin{}, {} {}’, format(self.vin, self.make, self.model). The format will replace the empty place holders with the respective values of the variables as defines within the format.
Once you have done this, run it again and you will notice a much better and a clear output that is free from any undesired hashcodes.
Calling from external program
So far you have written the code and tested it from within the program itself. Next, you will have to try and call your program from an external program. When you do that, the program would execute all the code that you have defined within this program and execute it accordingly. However, before you do that, you need to add an if statement before before the execution to check what the name space is. Just add “if__name__= = ‘__main__’ ;” Adding this additional statement to your code will make sure that this program will be executed only when the namespace is ‘__main__’ This makes sure that if you are importing automobile.py then the values will not be printed.
Once you have added the additional line, it is a good practice to run it once again just to make sure everything is fine. Otherwise you may have trouble finding the errors. Before you call the program from any external program, if you need to add any additional instances, you need to add def always followed by self as the first argument and then the variables. For example: def make_noise(self); In the next line you can print out the output as print ‘Honk I am a {} {}’, format(self.make, self.model); To test this, add two additional lines below the testing code as follows ‘print a’ and ‘a.make_noise()’. The parenthesis here denote that you are referring to a function. Check the output to be sure everything is fine. You can repeat the same process to add more instances.
To call this program from another program, create a fresh module, name it as per your convenience and continue to write a code. Wherever you need to call the program ‘Automobile’, all you need to do is write import Automobile. In general, you will be importing the base name of the program without the ‘.py’ file extension. Once you have done this, you need to run it with the python interpreter to make sure there are no errors. Next, you can also check by instantiating an object for example “my_car = Automobile.Automobile(‘2387437’, ‘Mercedes’, ‘C320’). The first ‘Automobile’ here refers to the module and the second one refers to the class. To print this out, you can write ‘print my_car’ and also add my_car.make_noise(). Run this and make sure there are no errors.
Please Share This Knowledge With Others!