|
1 |
| -# Python_Classes |
2 |
| -## Basics of Python Classes with examples |
| 1 | +# Basics of Python Objects and Classes |
| 2 | + |
| 3 | +Python is an object oriented programming language, which as you can guess, stresses on objects whereas procedure oriented programming stresses on functions. Objects are simply a collection of attributes (variables) and methods (functions) that act on those data and a class is a blueprint for that object. Python classes can be thought of as blueprints of a house, and objects can be thought of as a particular instance of that house (there can be multiple objects for one class, while they all may differ in number of bedrooms/bathrooms/etc., they will all have the same blueprint of the class). |
| 4 | + |
| 5 | +In computer programming, classes are a great way to organize attributes (variables) and methods (functions) so that they are easy to reuse and extend later. In this post, we will walk through how to build a basic class in python. Specifically, we will discuss the example of implementing a class that represents company employees. |
| 6 | + |
| 7 | +``` |
| 8 | +class Employee: |
| 9 | + pass |
| 10 | +
|
| 11 | +employee1=Employee() |
| 12 | +employee2=Employee() |
| 13 | +
|
| 14 | +print("Employee 1 is", employee1) |
| 15 | +print("Employee 2 is", employee2) |
| 16 | +``` |
| 17 | + |
| 18 | +We have created an Employee class, then instantiated an *employee1* and *employee2* object using the Employee class. |
| 19 | + |
| 20 | +``` |
| 21 | +class Employee: |
| 22 | + pass |
| 23 | +
|
| 24 | +employee1=Employee() |
| 25 | +employee1.name='Matt' |
| 26 | +employee1.income=50000 |
| 27 | +
|
| 28 | +print(employee1.name) |
| 29 | +print(employee1.income) |
| 30 | +
|
| 31 | +employee2=Employee() |
| 32 | +employee2.name='Penelope' |
| 33 | +employee2.income=90000 |
| 34 | +
|
| 35 | +print(employee2.name) |
| 36 | +print(employee2.income) |
| 37 | +``` |
| 38 | + |
| 39 | +Here we created attributes **name** and **income** for our *employee1* and *employee2* objects. This is however not ideal as it is not efficient, to correct this, we can use the ```__init__()``` method to define attributes automatically as we instantiate objects. |
| 40 | + |
| 41 | +``` |
| 42 | +class Employee: |
| 43 | + def __init__(self, name, income): |
| 44 | + self.name=name |
| 45 | + self.income=income |
| 46 | +
|
| 47 | +employee1=Employee('Matt',50000) |
| 48 | +print(employee1.name) |
| 49 | +print(employee1.income) |
| 50 | +
|
| 51 | +employee2=Employee('Penelope',90000) |
| 52 | +print(employee2.name) |
| 53 | +print(employee2.income) |
| 54 | +
|
| 55 | +# OUTPUT |
| 56 | +Matt |
| 57 | +50000 |
| 58 | +Penelope |
| 59 | +90000 |
| 60 | +``` |
| 61 | + |
| 62 | +We can have multiple methods within a class, so let's also create a method that determines an employees level of earning based on their **income**. Let's see what level of earning Matt and Penelope are by calling the *earning* method. |
| 63 | +``` |
| 64 | +class Employee: |
| 65 | + def __init__(self, name, income): |
| 66 | + self.name=name |
| 67 | + self.income=income |
| 68 | + |
| 69 | + def earning(self): |
| 70 | + if self.income >= 80000: |
| 71 | + return 'High Earning' |
| 72 | + elif (self.income<8000) & (self.income>50000): |
| 73 | + return "Medium Earning" |
| 74 | + else: |
| 75 | + return "Low Earning" |
| 76 | +employee1=Employee('Matt',50000) |
| 77 | +level_earning=employee1.earning() |
| 78 | +print(employee1.name, "=", level_earning) |
| 79 | +employee2=Employee('Penelope',90000) |
| 80 | +level_earning=employee2.earning() |
| 81 | +print(employee2.name, "=", level_earning) |
| 82 | +# Output |
| 83 | +Matt = Low Earning |
| 84 | +Penelope = High Earning |
| 85 | +
|
| 86 | +#OUTPUT |
| 87 | +Matt = Low Earning |
| 88 | +Penelope = High Earning |
| 89 | +``` |
| 90 | + |
| 91 | +As you can see, calling the earning method on the employee1 and employee2 objects tells us what level of earning each employee is at. |
| 92 | +To review, we have created a class with two methods, an **__init__()** method that initializes attributes of name and income and an earning method that return the level of earning depending on a persons income. We then created two objects, *employee1* with the parameters “Matt” and 50000, when these objects were created the **__init__()** method is called and name and income of the *employee1* object are now “Matt” and 50000. Then we checked what level of earning *employee1* is at and printed the results. We followed the same procedure when creating the *employee2* object. |
0 commit comments