Python Objects and Their Use

With today's lesson we will start learning object oriented in Python by introducing Objects in Python.

  • Objects are the fundamental component of object-oriented programming.

In this lesson we will discuss about the Objects only, in later lectures I will introduce you the Object Oriented Python.

  • All objects have certain attributes and behavior.

Let's examine the real life example: Our object is a Car;

Attributes:

  • It's color
  • Number of miles driven
  • current location

Behaviours:

  • Driving the car (Change the miles driven, change location)
  • Painting the car (Changing the color)

In similar fashion, Objects in computer science contains a set of attributes, stored in a set of instance variables, and a set of functions called methods that provide its behaviour.

For instance, when we talked about lists, we mentioned sort(list) function. It's a procerdural programming approach with two distinct entities a sort function and a list to pass it.

But in Object-oriented programming approach, the sort routine would be part of the object containing the list like other methods for lists;

  • append(x)
  • remove(x)
  • reverse()
  • sort()

In [5]:
lst = [10, 4, 7, 8, 9, 13, 1, 0, 78, 5, 58]

In [6]:
lst.append(55)
lst


Out[6]:
[10, 4, 7, 8, 9, 13, 1, 0, 78, 5, 58, 55]

In [7]:
lst.remove(4)
lst


Out[7]:
[10, 7, 8, 9, 13, 1, 0, 78, 5, 58, 55]

In [8]:
lst.reverse()
lst


Out[8]:
[55, 58, 5, 78, 0, 1, 13, 9, 8, 7, 10]

In [9]:
lst.sort()
lst


Out[9]:
[0, 1, 5, 7, 8, 9, 10, 13, 55, 58, 78]

In Python, objects are represented as a reference to an object in memory.

A reference is a value that references, or “points to,” the location of another entity.T hus, when a new object in Python is created, two entities are stored—the object, and a variable holding a reference to the object. All access to the object is through the reference value.

We can get the reference value of a variable (that is, the location in which the corresponding object is stored) by use of built-in function id.


In [19]:
n = 10
id(n)


Out[19]:
26488992

In [20]:
k = 10
id(k)


Out[20]:
26488992

In [22]:
s = 20
id(s)


Out[22]:
26488752

After introducing the basic concept of objects, now we can continue exploring Python. In this section we will look at what is modular desing, why is important and how to do it in Python

Modular Design

Modular Design for Orbiter Flight Computer Software:

An important aspect of well-designed software is that programs are designed as a collection of modules. The term “module” broadly speaking, refers to the design and/or implementation of specific functionality to be incorporated into a program.While an individual function may be considered a module, modules generally consists of a collection of functions (or other entities).

Every module needs to provide a specifi cation of how it is to be used. This is referred to as the module’s interface.

A docstring is a string literal denoted by triple quotes given as the first line of certain program elements.


In [27]:
def numPrimes(start, end):
    """ Returns the number of primes between start and end. """
    return 0

In [28]:
print(numPrimes.__doc__)


 Returns the number of primes between start and end. 
  • A Python module is a file containing Python definitions and statements.
  • The Python Standard Library contains a set of predefined standard (built-in) modules.

  • A namespace provides a context for a set of identifiers. Every module in Python has its own namespace.
  • A name clash is when two otherwise distinct entities with the same identifier become part of the same scope.


With the import modulename form of import in Python, the namespace of the imported module becomes available to, but does not become part of, the namespace of the importing module.


In [29]:
import math
factortial(5)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-29-b7fec7ccd69b> in <module>()
      1 import math
----> 2 factortial(5)

NameError: name 'factortial' is not defined

In [30]:
import math 
math.factorial(5)


Out[30]:
120

The "from-import" Form of Import

With the from-import form of import, imported identifiers become part of the importing module’s namespace. Because of the possibility of name clashes, import modulename is the preferred form of import in Python.


In [32]:
from math import factorial
factorial(5) # Works but vulnerable to name clashes


Out[32]:
120

In [33]:
def factorial(n):
    print('my factorial function')

In [35]:
factorial(5) # It's not what I wanted


my factorial function

In [36]:
math.factorial(5)


Out[36]:
120

In [37]:
from math import factorial as fact
fact(5)


Out[37]:
120

In [38]:
def factorial(n):
    print('my factorial function')

In [39]:
factorial(5)


my factorial function

In [40]:
fact(5)


Out[40]:
120