Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 3.1: OOP

2 Replies, 1644 Views

[Image: BFMMlbcQvml9HSqXcvNp]
Object Oriented Programming (OOP):
                       Hi guys, I hope you all are doing well. In this thread we are going to have a look at object oriented programming and some of its basic concepts and then we will go through some advanced concepts in the next coming threads. So without wasting any time let’s jump into it..

So the first thing in this concept is the objects. We have worked a lot with objects but we never knew them in procedural programming or functional programming but here we will see what these objects were. And how we can create our own objects. For Example look at the code below

Code:
x = 10
print(type(x))

Output: <class 'int'>


So this code will return the type of x. We have worked with integers, strings, floats and functions etc. Actually they all were classes and we were creating their objects. They were built in classes and we were just using them. Classes are like blueprints and objects are instances of these classes.

So I hope you have understood the concept of class and object. We will not jump into making our own class directly as this is for beginners so first of all concept of these things is must so now we will see another concept which is methods. Methods are operations that we perform on objects of a specific class. For Example 

Code:
comp = "red sec"
print(comp.upper())


Now, we have created a string object named ‘comp’. The upper() is a method here which is performing some action on our object. lower() is also a method. Note you can not apply these methods on other classes because it is only defined for string class.

Hope you understand these concepts now. So I am going to show you how you can create your own classes, their objects and methods for them.

Classes are created by using a keyword class and indented block of code which include methods (actually functions). So here is an example below for you.

All iphones are different objects. Your iphone is a different object. Mine is a different one. But all of them belong to the iphone class. All of them are iphones. Right? You get it. That's what class names are. Now we will create its blueprint, suppose our Iphone class will have objects having a camera, a color and screen.'''

Code:
class Iphone:
  def __init__(self, camera, color, screen):
      self.camera = camera
      self.color = color
      self.screen = screen


    init function is our blueprint. Now, we have defined properties here. But what is self? Well, valid doubt. Now think of it, why do we create this blueprint? So that we can use it to create as many objects of this class as we want. Right? Suppose we use this blueprint of iphone class to create your iphone object, then self is the object we create at that time. The current object. So, your phone is self. and self has these properties of blueprint.

      Now, when my iPhone is created using this blueprint, self is my phone and it has all the properties described. people often get confused, why do we write self.camera = camera. Well, now that you know that self is the iphone object being created using blueprint. read it as this. Self.camera means self will have a camera property which will be equal to the value we assign to it using this blueprint at the time of creation. Similarly,  properties work. Now that we have prepared our blueprint, let's create an iphone.

Code:
YourPhone = Iphone ("30MP", "black", "6 inches")


Wow, we have created our first object using this blueprint. Read it as this. We are creating a YourPhone object, which is an object of iphone class that means it will have properties defined in the class blueprint. at the time of creation, we can assign any value to properties. Like if we want, we can give it 23MP to the camera or 40MP or 30MP. set its color to any of our choices. We can set screen size to anything.

Let’s see this example in this example we create an class of students

Code:
class Students:
    def __init__(self, name, age, rollNumber):
        self.name = name
        self.age = age
        self.rollNumber = rollNumber

    def homework(self):
        print("doing homework")

    def study(self):
        print("Studying")

kurd = Students("Kurd", 34, '1234')

print(kurd.rollNumber)
kurd.study()

And that’s it for now try to create more and more classes for your practice and it will also build up your knowledge and search for more ideas about classes.


"Keep Good care of your Health
Allah Hafiz and Good Bye Till the next post"
Love me like you do Heart
(This post was last modified: 05-23-2022, 09:23 PM by Covid-19.)
This is one of the most important subjects to learn about any progranming language... otherwise you will have to deal with regular objects and lists.
Rs
* Thankful to Allah *
Kurdy
(05-24-2022, 01:43 PM)Mr.Kurd Wrote: This is one of the most important subjects to learn about any progranming language... otherwise you will have to deal with regular objects and lists.

Absolutely it helps a lot in making programming understandable
Love me like you do Heart

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,247 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,638 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,670 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,843 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,607 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,841 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,792 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,833 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,825 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,827 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)