With today's lesson we will start learning object oriented in Python by introducing Objects in Python.
In this lesson we will discuss about the Objects only, in later lectures I will introduce you the Object Oriented Python.
Let's examine the real life example: Our object is a Car;
Attributes:
Behaviours:
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]:
In [7]:
lst.remove(4)
lst
Out[7]:
In [8]:
lst.reverse()
lst
Out[8]:
In [9]:
lst.sort()
lst
Out[9]:
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]:
In [20]:
k = 10
id(k)
Out[20]:
In [22]:
s = 20
id(s)
Out[22]:
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 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__)
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)
In [30]:
import math
math.factorial(5)
Out[30]:
In [32]:
from math import factorial
factorial(5) # Works but vulnerable to name clashes
Out[32]:
In [33]:
def factorial(n):
print('my factorial function')
In [35]:
factorial(5) # It's not what I wanted
In [36]:
math.factorial(5)
Out[36]:
In [37]:
from math import factorial as fact
fact(5)
Out[37]:
In [38]:
def factorial(n):
print('my factorial function')
In [39]:
factorial(5)
In [40]:
fact(5)
Out[40]: