Mostly we have designed our python program around functions or 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 what is called an object. This is called the object oriented programming paradigm. Most of the time you can use procedural programming but sometimes when you want to write large programs or have a solution that is better suited to it, you can use object oriented programming techniques.
Classes and objects are the two main aspect of object oriented programming.
- A class creates a new type where objects are instances of the class. An analogy is that you can have variables of type
int which translates to saying that variables that store integers are variables which are instances (objects) of the int class.
- Objects can store data using ordinary variables that belong to the object. Variables that belong to an object or class are called as fields.
- Objects can also have functionality by using functions that belong to a class. Such functions are called methods of the class.
- This terminology is important because it helps us to differentiate between functions and variables which are separate by itself and those which belong to a class or object.
- Collectively, the fields and methods can be referred to as the attributes of that class.
Fields are of two types - they can belong to each instance/object of the class or they can belong to the class itself. They are called instance variables and class variables respectively.
A class is created using the class keyword. The fields and methods of the class are listed in an indented block.
Now Python Class Have One special SELF parameter so let's understand it first :
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 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.
How it works :
You must be wondering how Python gives the value for self and why you don't need to give a value for it.
An example will make this clear. Say you have a class called MyClass and an instance of this class called MyObject. When you call a method of this object as MyObject.method(arg1, arg2), this is automatically converted by Python into MyClass.method(MyObject, arg1, arg2 - this is what the special self is all about.
This also means that if you have a method which takes no arguments, then you still have to define the method to have a self argument.
Now Classes
The simplest class possible is shown in the following example. Example Creating a Class
#!/usr/bin/python
class Person:
pass
p = Person()
print p
$ python simplestclass.py
<__main__.Person instance at 0xf6fcb18c>
We create a new class using the class statement followed by the name of the class. This follows an indented block of statements which form the body of the class. In this case, we have an empty block which is indicated using the pass statement.
Next, we create an object/instance of this class using the name of the class followed by a pair of parentheses. For our verification, we confirm the type of the variable by simply printing it. It tells us that we have an instance of the Person class in the __main__ module.
Notice that the address of the computer memory where your object is stored is also printed. The address will have a different value on your computer since Python can store the object wherever it finds space.
object Methods
We have already discussed that classes/objects can have methods just like functions except that we have an extra self variable. We will now see an example.
Example Using Object Methods
class Person:
def sayHi(self):
print 'Hello, how are you?'
p = Person()
p.sayHi()
$ python method.py
Hello, how are you?
How It Works
Here we see the self in action. Notice that the sayHi method takes no parameters but still has the self in the function definition.
Now this was very basic and important towards OOPS Programming with Python for more you can refer the below link :
Regards