|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Horizontal scroll box" |
| 4 | +date: 2019-09-19 22:07:39 -0700 |
| 5 | +category: Python |
| 6 | +img: /static/IMG/html1.jpg |
| 7 | +color: green theme_color: "#4CAF50" |
| 8 | +Prism: |
| 9 | +tags: |
| 10 | +- Python |
| 11 | +- Tutorial |
| 12 | +--- |
| 13 | + |
| 14 | +# Python Object Oriented Programming |
| 15 | +As we all know Python is Object Oriented Programming language. But you won't believe we are using Python OOP from our first program itself. For example if you try following snippet you will get what I am saying. |
| 16 | + |
| 17 | +<pre class="line-numbers"> |
| 18 | +<code class="python"> |
| 19 | +x = 2 |
| 20 | +print(type(x)) |
| 21 | +</code> |
| 22 | +</pre> |
| 23 | + |
| 24 | +It will give you following result: |
| 25 | +```<class 'int'>``` |
| 26 | + |
| 27 | +**You can see this in any data item like list, tuple, dictionary sets ...etc**. That is each datatype is belongs to a class. For example **append** is a method within the class named list. We will discuss what is method in this post later. |
| 28 | + |
| 29 | +{% include MyNote.html note_type="Success" span_note="Important" text="In Python, all data items are objects, and each object is an instance of a class. The importance of classes is: by defining your own class, you define a fundamental new data type." %} |
| 30 | + |
| 31 | +## How do we create simple class? |
| 32 | +Using class keyword we can create new class. The syntax of class is given below: |
| 33 | +<pre class="line-numbers"> |
| 34 | +<code class="python"> |
| 35 | +class class_name: |
| 36 | + method_definitions |
| 37 | +</code> |
| 38 | +</pre> |
| 39 | + |
| 40 | +Let us create our own class, |
| 41 | +<pre class="line-numbers"> |
| 42 | +<code class="python"> |
| 43 | +class Employee: |
| 44 | + pass |
| 45 | +</code> |
| 46 | +</pre> |
| 47 | + |
| 48 | +{% include MyNote.html note_type="Info" span_note="Info" text="The keyword pass is a kind of placeholder. It says, “There’s nothing more to do here for now; I’ll come back and add things later.” Or, you might just use it as a permanent no-op (no operation). Occasionally this is needed because Python has no statement terminator; therefore, it has no way to specify a blank statement other than **pass**." %} |
| 49 | + |
| 50 | +Let us create a new objects of our class Employee: |
| 51 | +<pre class="line-numbers"> |
| 52 | +<code class="python"> |
| 53 | +employee1 = Employee() |
| 54 | +employee2 = Employee() |
| 55 | +</code> |
| 56 | +</pre> |
| 57 | + |
| 58 | +Let us check whether we are created instances of class Employee successfully: |
| 59 | +<pre class="line-numbers"> |
| 60 | +<code class="python"> |
| 61 | +print(type(employee1)) |
| 62 | +</code> |
| 63 | +</pre> |
| 64 | + |
| 65 | +This will give you something like ```<class '__main__.Employee'>```. Then it is perfect. |
| 66 | + |
| 67 | +Now it is the time to create instance variables. Instance variables are used for attach data to individual objects. Using these instance variable we can attach data field directly to at runtime. Instances are written outside class. |
| 68 | + |
| 69 | +<pre class="line-numbers"> |
| 70 | +<code class="python"> |
| 71 | +employee1.firstName = 'Arun' |
| 72 | +employee1.lastName = 'Soman' |
| 73 | +employee1.email = employee1.firstName + employee1.lastName+'@company.com' |
| 74 | + |
| 75 | +employee2.firstName = 'Anuja' |
| 76 | +employee2.lastName = 'Soman' |
| 77 | +employee2.email = employee2.firstName + employee2.lastName+'@company.com' |
| 78 | + |
| 79 | +print(employee1.email) |
| 80 | +print(employee2.email) |
| 81 | +</code> |
| 82 | +</pre> |
| 83 | + |
| 84 | +Our primary concern is to reduce number of lines in the program. So it is not a good way to write program. In next post we will discuss about ***self obsession*** and how to reduce number of lines in the program using Python **default constructor** called *__init__* . |
| 85 | + |
| 86 | +## Writing Methods Inside Class |
| 87 | +Writing a method is how we give objects of a class the ability to respond to messages; another way of saying this is that methods give objects of a class behavior. |
| 88 | + |
| 89 | + {% include MyNote.html note_type="info" span_note="Info: " text="In Python, as elsewhere, methods are functions defined inside class definitions—even though they may be called through an object (an “instance method”)." %} |
| 90 | + |
| 91 | +Python supports following types of methods |
| 92 | +1. Regular Methods |
| 93 | +2. Class Methods and |
| 94 | +3. Static Methods |
| 95 | + |
| 96 | +We will discuss about all of the above methods in upcoming blog posts. Let us create a regular method by extending our previous program. |
| 97 | + |
| 98 | +<pre class="line-numbers"> |
| 99 | +<code class="python"> |
| 100 | +class Employee: |
| 101 | + def fullName(firstName, lastName): |
| 102 | + return firstName + ' '+ lastName |
| 103 | + |
| 104 | + |
| 105 | +# instances or objects |
| 106 | +employee1 = Employee() |
| 107 | +employee2 = Employee() |
| 108 | + |
| 109 | +# instance variables of employee1 |
| 110 | +employee1.firstName = 'Arun' |
| 111 | +employee1.lastName = 'Soman' |
| 112 | + |
| 113 | +# instance variables of employee2 |
| 114 | +employee2.firstName = 'Anuja' |
| 115 | +employee2.lastName = 'Soman' |
| 116 | + |
| 117 | +# calling method in the class |
| 118 | +print(Employee.fullName(employee1.firstName,employee1.lastName)) |
| 119 | + |
| 120 | +# calling method in the class |
| 121 | +print(Employee.fullName(employee2.firstName,employee2.lastName)) |
| 122 | +</code> |
| 123 | +</pre> |
| 124 | + |
| 125 | +In the program given above, we created an Employee class. In the main program we can see instances of Employee class. Using those instances we created new instance variables called firstname and lastname. Next we wrote a method called fullname in the class in order to concatenate firstname and lastname. In the last two print statement the method fullname is called. As I mentioned earlier, we cooked up this program by writing instance variable over and over again which is not a good idea. How to eradicate this problem? See our next post for solution. |
| 126 | + |
| 127 | +Reference: |
|
0 commit comments