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
Theself
in Python is equivalent to thethis
pointer in C++ and thethis
reference in Java and C#.
Note for C++/Java/C# Programmers
Let's look at this simple class
Basic Class
Output: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()
$ 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.
Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://python.swaroopch.com/oop.html
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit