[Python] Important to know, OOP(Object-oriented Programming) tutorial

in #python6 years ago (edited)

[Video tutorial link]
After (or before) reading tutorial, don't forget to watch this beautiful, well-explained tutorial which was my main reason to write this post. I think it's the best tutorial ever for Python OOP.

What is Python?

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.
For me, Python is the best programming language to start with. It's simple syntax makes Python beginner-friendly.

What is Object-oriented programming (so called OOP)?

In all the programs we wrote till now, we have designed our program around functions i.e. blocks of statements which manipulate data. This is called the procedure-oriented way of programming. There is another way of organizing your program which is to combine data and functionality and wrap it inside something called an object. This is called the object oriented programming paradigm. Most of the time you can use procedural programming, but when writing large programs or have a problem that is better suited to this method, you can use object oriented programming techniques.

The self

Class methods have only one specific difference from ordinary functions - they must have an extra first name that has to be added to the beginning of the parameter list, but you do not give a value for this parameter when you call the method, Python will provide it. This particular variable refers to the object itself, and by convention, it is given the name self
Although, you can give any name for this parameter, it is strongly recommended that you use the name self - any other name is definitely frowned upon. There are many advantages to using a standard name - any reader of your program will immediately recognize it and even specialized IDEs (Integrated Development Environments) can help you if you use self

The self in Python is equivalent to the thispointer in C++ and the this reference in Java and C#.

Note for C++/Java/C# Programmers


Let's look at this simple class

Basic Class

class Person:
    def __init__(self, name):
        self.name = name
    def say_hi(self):
        print('Hello, my name is', self.name)
p = Person('Kenan')

p.say_hi()
Output:
$ python oop_init.py
Hello, my name is Kenan

How It Works:

Here, we define the __init__ method as taking a parameter name (along with the usual self). Here, we just create a new field also called name. Notice these are two different variables even though they are both called 'name'. There is no problem because the dotted notation self.name means that there is something called "name" that is part of the object called "self" and the other name is a local variable. Since we explicitly indicate which name we are referring to, there is no confusion.

When creating new instance p, of the class Person, we do so by using the class name, followed by the arguments in the parentheses: p = Person('Kenan').

We do not explicitly call the __init__method. This is the special significance of this method.

Now, we are able to use the self.name field in our methods which is demonstrated in the say_hi method.




Read the full article from here.

Coin Marketplace

STEEM 0.28
TRX 0.13
JST 0.032
BTC 61626.58
ETH 2940.28
USDT 1.00
SBD 3.66