Object-oriented programming in Python.docx -...

This preview shows page 1 - 4 out of 14 pages.

Object-oriented programming in Python last modified July 6, 2020 In this part of the Python tutorial, we talk about object-oriented programming in Python. There are three widely used programming paradigms there: procedural programming, functional programming, and object- oriented programming. Python supports all three programming paradigms. Object-oriented programming Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. There are some basic programming concepts in OOP: Abstraction Polymorphism Encapsulation Inheritance The abstraction is simplifying complex reality by modeling classes appropriate to the problem. The polymorphism is the process of using an operator or function in different ways for different data input. The encapsulation hides the implementation details of a class from other objects. The inheritance is a way to form new classes using classes that have already been defined. Python objects Everything in Python is an object. Objects are basic building blocks of a Python OOP program.
object_types.py #!/usr/bin/env python # object_types.py import sys def function(): pass print(type(1)) print(type("")) print(type([])) print(type({})) print(type(())) print(type(object)) print(type(function)) print(type(sys)) In this example we show that all these entities are in fact objects. The type() function returns the type of the object specified. $ ./object_types.py <class 'int'> <class 'str'> <class 'list'> <class 'dict'> <class 'tuple'> <class 'type'> <class 'function'> <class 'module'> Integers, strings, lists, dictionaries, tuples, functions, and modules are Python objects. Python class keyword The previous objects were all built-in objects of the Python programming language. The user defined objects are created using the class keyword. The class is a blueprint that defines a nature of a future object. From classes we construct instances. An instance is a specific object created from a particular class. For example, Huck might be an instance of a Dog class. first_object.py #!/usr/bin/env python # first_object.py class First:
pass fr = First() print(type(fr)) print(type(First)) This is our first class. The body of the class is left empty for now. It is a convention to give classes a name that starts with a capital letter. class First: pass Here we define the First class. Note that by default, all classes inherit from the base object . fr = First() Here we create a new instance of the First class. Or in other words, we instantiate the First class. The fr is a reference to our new object. $ ./first_object.py <class '__main__.First'> <class 'type'> Here we see that fr is an instance object of the First class. Inside a class, we can define attributes and methods. An attribute is a characteristic of an object. This can be for example a salary of an employee. A method defines operations that we can perform with our objects. A method might define a cancellation of an account. Technically, attributes are variables and methods are functions defined inside a class.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture