Brief introduction
So far, in our programs, we have designed programs based on the function or block of statements that manipulate the data. This is referred to as process-oriented programming. There is also a way to combine data and functionality to organize programs by wrapping things called objects. This approach is called object-oriented programming philosophy. Most of the time you can use procedural programming, but sometimes when you want to write large programs or find a more appropriate solution, you have to use object-oriented programming techniques.
Classes and objects are the two main aspects of object-oriented programming. Class to create a new type, and an instance of this class of objects. This is similar to having a variable of type int, which stores the integer variable as an instance of the Int class (object).
Comments to c/c++/java/c# Programmers
Note that even integers are treated as objects (belonging to the Int class). This is not the same as C + +, Java (prior to 1.5) when integers are purely as types. Learn more about this class by using Help (int). C # and Java 1.5 Programmers are familiar with this concept because it resembles the concept of encapsulation and solution encapsulation.
objects can store data using ordinary variables that belong to an object. A variable that belongs to an object or class is called a domain. Objects can also have functionality by using functions that are part of the class. Such a function is called a method of a class. These terms help us distinguish them from isolated functions and variables. Fields and methods can be combined as properties of a class.
There are two types of fields--objects that belong to each instance/class or belong to the class itself. They are called instance variables and class variables, respectively.
Class is created using the Classes keyword. The fields and methods of the class are listed in a indented block.
Self
The methods of a class have only one particular difference from a normal function-they must have an extra first parameter name, but you don't assign a value to this parameter when you call the method, and Python provides that value. This particular variable refers to the object itself, and by convention its name is self.
Although you can give this parameter any name, it is strongly recommended that you use the name self-other names are not in favor of your use. There are many advantages to using a standard name--your program readers can quickly identify it, and if you use self, there are also IDES (integrated development environments) that can help you.
Comments to c++/java/c# Programmers
Self in Python is equivalent to the self pointer in C + + and the This reference in Java, C #.
You must be wondering how Python assigns a value to self and why you don't need to assign it a value. An example would make this clear. Suppose you have a class called MyClass and an instance of this class MyObject. When you call the object's method Myobject.method (Arg1, arg2), it automatically transitions from Python to Myclass.method (MyObject, Arg1, arg2)-that's the principle of self.
This also means that if you have a method that does not require a parameter, you still have to define a self parameter for the method.
Class
A class as simple as possible is shown in the following example.
Create a class
Example 11.1 to create a class
#!/usr/bin/python
# Filename: simplestclass.py
class Person:
pass # An empty block
p = Person()
print p