Like other programming languages, Python language also supports object oriented programming. Object orientation can help to reduce complexity, particularly in long programs. It also encourages
programmers to re-use existing, tested code because it is possible to create objects that inherit the
majority of their attributes and methods from an existing class, leaving the programmer to add or
override the pieces that specifically need to be different. Using the principle known as inheritance,
specific objects can be created out of general ones, producing a family tree of objects similar to the
classification of natural species.
Let's first understand what object-oriented programming is. Before this concept was introduced, we were coding in procedural programming, that is, going line by line. At this level, you need not understand what is procedural programming but certainly there is one example to illustrate it, that is, C language. In procedural programming, there were a lot of complexities and above all procedural programming had negligible code reuse concept.
The concept of object-oriented programming was seen to solve many problems, which procedural programming did not solve. In object-oriented programming, everything mimics just like a real-world object. In the real world, everything is an object. An object can have state and behavior. An object in the real world can communicate with another object. For example, a shape object in the real world has state and behavior. OOPs is based on four pillars. They are:
Key concepts
Actually OOPs is a vast topic that needs to be covered at the intermediate level. In case of Python
programming, however, we will cover some key concepts which will make programming using OO approach easier.
1. Class: Class is considered as a blueprint for object creation. It can be understood as a factory to produce objects. It provides a template for creating an object and specifying its behavior through means of methods and state through means of variable instance name.
2. Objects: An object is an instance of a class. An object can have state and behavior or attributes. The objects basically bundles a set of related states and behaviors, for example, a shape has state (name) and behavior (round, square).
3. Inheritance: In object-oriented programming, the child class can inherit many properties from the parent class. Here, we mean that the child class can use an existing method or behavior, which the parent class has defined and use them accordingly in their class. Inheritance can be a single inheritance or multiple inheritance. Single inheritance, as the name suggests, refers to only one parent, while multiple inheritance refers to inheriting the property from multiple parents.
4. Polymorphism: In OOPs, an object can have many forms through means of different attributes. To
simplify, in our case, we can understand it by methods with the same name but having different outputs.
5. Abstraction: Here, we hide the necessary details and are only interested in showing the relevant details to the other intended user. Here, by other intended user we mean another software application, or another class, or other client who will be the end users of the program.
6. Encapsulation: This refers to hiding the necessary methods and their relevant details from the outside world. A class can be treated as a best example, which provides encapsulation to the methods and relevant instances.
Object orientation can help to reduce complexity, particularly in long programs. It also encourages
programmers to re-use existing, tested code because it is possible to create objects that inherit the
majority of their attributes and methods from an existing class, leaving the programmer to add or
override the pieces that specifically need to be different. Using the principle of inheritance, specific objects can be created out of general ones, producing a family tree of objects similar to the classification of natural species.
Central to the concept of object orientation is the class—a template for creating your own data types. A data type may be a person, for example, each of whom has unique aspects that might be specified by data such as first_name, last_name, Age, Sex, and Location.
Think of a class as a noun, representing objects that are described by unique aspects. These aspects are technically referred to in OOP as attributes. Class definitions are sometimes referred to as templates, as their entire purpose is to serve as a blueprint or model of class objects for you to use in your programs. Classes form the basis of a whole new style of programming. You have already moved the focus of your programs from simple top -down scripts to function-driven applications. Classes enable you to move the action into custom-built objects, which can contain both data (attributes) and commands (methods). Creating objects involves a further stage of abstraction from function-driven programming and provides a means of modeling real-world constructs as single entities in your code.
Objects are the primary means of representing data in Python. All data in a Python program is represented by some type of object or by the relationship between objects. A class is a template for an object that bundles up several related pieces of data and defines a bunch of functions for accessing and manipulating that data. A class definition is similar to a function definition in that nothing happens until you tell Python to make an object based on the class template; this is called creating an instance of that class. Even then, any methods that are part of the class won't do anything until they are specifically called, just like functions.
Organizing your data into self contained units makes it easier to figure out problems using the same sort of logic that you are used to using in real-world situations. Many programmers find that object orientation allows their code to grow in a more natural way than constantly having to create new functions from scratch.
Lets say we have a class Pizza. This would contain general attributes that could be applied to any object of the same class. You could then derive further subclasses from these base class, so Pizza could become the parent to several child classes, such as Veg and Non-veg. Each of these children would contain new attributes specific to their type, but they’d also inherit the general attributes of a Pizza.
Here our introduction to Python as a OOL comes to an end. Next we'll start using the concepts introduced in this post to make programs. I'd suggest to read in more details about OOP as in real world programs we use OOP.
So keep learning Python as Python is easy to learn!
programmers to re-use existing, tested code because it is possible to create objects that inherit the
majority of their attributes and methods from an existing class, leaving the programmer to add or
override the pieces that specifically need to be different. Using the principle known as inheritance,
specific objects can be created out of general ones, producing a family tree of objects similar to the
classification of natural species.
Let's first understand what object-oriented programming is. Before this concept was introduced, we were coding in procedural programming, that is, going line by line. At this level, you need not understand what is procedural programming but certainly there is one example to illustrate it, that is, C language. In procedural programming, there were a lot of complexities and above all procedural programming had negligible code reuse concept.
The concept of object-oriented programming was seen to solve many problems, which procedural programming did not solve. In object-oriented programming, everything mimics just like a real-world object. In the real world, everything is an object. An object can have state and behavior. An object in the real world can communicate with another object. For example, a shape object in the real world has state and behavior. OOPs is based on four pillars. They are:
- Polymorphism
- Inheritance
- Abstraction
- Encapsulation
Key concepts
Actually OOPs is a vast topic that needs to be covered at the intermediate level. In case of Python
programming, however, we will cover some key concepts which will make programming using OO approach easier.
1. Class: Class is considered as a blueprint for object creation. It can be understood as a factory to produce objects. It provides a template for creating an object and specifying its behavior through means of methods and state through means of variable instance name.
2. Objects: An object is an instance of a class. An object can have state and behavior or attributes. The objects basically bundles a set of related states and behaviors, for example, a shape has state (name) and behavior (round, square).
3. Inheritance: In object-oriented programming, the child class can inherit many properties from the parent class. Here, we mean that the child class can use an existing method or behavior, which the parent class has defined and use them accordingly in their class. Inheritance can be a single inheritance or multiple inheritance. Single inheritance, as the name suggests, refers to only one parent, while multiple inheritance refers to inheriting the property from multiple parents.
4. Polymorphism: In OOPs, an object can have many forms through means of different attributes. To
simplify, in our case, we can understand it by methods with the same name but having different outputs.
5. Abstraction: Here, we hide the necessary details and are only interested in showing the relevant details to the other intended user. Here, by other intended user we mean another software application, or another class, or other client who will be the end users of the program.
6. Encapsulation: This refers to hiding the necessary methods and their relevant details from the outside world. A class can be treated as a best example, which provides encapsulation to the methods and relevant instances.
Object orientation can help to reduce complexity, particularly in long programs. It also encourages
programmers to re-use existing, tested code because it is possible to create objects that inherit the
majority of their attributes and methods from an existing class, leaving the programmer to add or
override the pieces that specifically need to be different. Using the principle of inheritance, specific objects can be created out of general ones, producing a family tree of objects similar to the classification of natural species.
Central to the concept of object orientation is the class—a template for creating your own data types. A data type may be a person, for example, each of whom has unique aspects that might be specified by data such as first_name, last_name, Age, Sex, and Location.
Think of a class as a noun, representing objects that are described by unique aspects. These aspects are technically referred to in OOP as attributes. Class definitions are sometimes referred to as templates, as their entire purpose is to serve as a blueprint or model of class objects for you to use in your programs. Classes form the basis of a whole new style of programming. You have already moved the focus of your programs from simple top -down scripts to function-driven applications. Classes enable you to move the action into custom-built objects, which can contain both data (attributes) and commands (methods). Creating objects involves a further stage of abstraction from function-driven programming and provides a means of modeling real-world constructs as single entities in your code.
Objects are the primary means of representing data in Python. All data in a Python program is represented by some type of object or by the relationship between objects. A class is a template for an object that bundles up several related pieces of data and defines a bunch of functions for accessing and manipulating that data. A class definition is similar to a function definition in that nothing happens until you tell Python to make an object based on the class template; this is called creating an instance of that class. Even then, any methods that are part of the class won't do anything until they are specifically called, just like functions.
Organizing your data into self contained units makes it easier to figure out problems using the same sort of logic that you are used to using in real-world situations. Many programmers find that object orientation allows their code to grow in a more natural way than constantly having to create new functions from scratch.
Lets say we have a class Pizza. This would contain general attributes that could be applied to any object of the same class. You could then derive further subclasses from these base class, so Pizza could become the parent to several child classes, such as Veg and Non-veg. Each of these children would contain new attributes specific to their type, but they’d also inherit the general attributes of a Pizza.
Here our introduction to Python as a OOL comes to an end. Next we'll start using the concepts introduced in this post to make programs. I'd suggest to read in more details about OOP as in real world programs we use OOP.
So keep learning Python as Python is easy to learn!
0 comments:
Post a Comment