A Quick Start Introduction to Python Programming - 5

Tuesday, September 16, 2008

In this concluding section (atleast for now), we will see more about object concepts in python and some introduction to python thread programming. From the last section on object oriented programming in python, I am sure that you would have understood something about class objects, instance objects and other OO concepts in python, an understanding of Object Oriented Programming in python is essential to explore the language further.

If you want to read this quick start introduction into python programming from the beginning, do visit "A Quickstart Introduction to Python Programming - 1".

As I always mentioned that everything in python is an object, it applies to everything like module attributes, functions, data types and infact importing a module in itself is an object which can be assigned to another python object as we will see below.


Listing 9:
object_concept.py


#
# Object concepts in python
# object_concept.py
# Author: S.Prasanna
#

import time

print "Todays date and time:", time.ctime(time.time())

print "time.time =", time.time
print "time.ctime =", time.ctime

# Get the time.time and time.ctime objects
timeobject = time.time
ctimeobject = time.ctime
print "Todays date and time:", ctimeobject(timeobject())

#Importing a module is also an object
timemodule = __import__("time")
print "Todays date and time:", timemodule.ctime(timemodule.time())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Output:

IDLE 1.2.2 ==== No Subprocess ====
>>>
Todays date and time: Tue Sep 16 00:57:44 2008
time.time =
time.ctime =
Todays date and time: Tue Sep 16 00:57:44 2008
Todays date and time: Tue Sep 16 00:57:44 2008
>>>

Explanation:

Line 8 Here we print the current date and time using the time.ctime function which takes a floating point number as argument which the time.time() returns. Then we print the time.time and time.ctime objects which are built in functions in the time module.

Line 14 - 17 Here we assign the time.time and time.ctime built in functions to two objects, namely the timeobject and ctimeobject respectively and then print the current date and time in a similar way, this goes to show that objects and other built in functions are similar to variables which can be assigned to other objects just as we assign primitive data types to variables.

Line 18 - 21 As I mentioned earlier importing a module is also an object, if you want to access the imported module, use the __import__ built in function which will return the imported module, then one can simply call the methods and access other attributes of the module using the object to which the imported module is assigned.

So whatever we have seen till now are some interesting object oriented concepts in python, but its just a tip of the iceberg, there are lots to learn in this interesting language.

10. Thread Programming in Python

Python has good thread routines like other high level languages and I have discussed them in another article here.

What next....

So far we have learned about some important concepts in python, though this is a good start, there are lots of other interesting features of python one can learn from many valuable online resources. There are good free books on python available in web and I have listed my top three python programming books which you should look into to take your skills to the next level, of course I would also be writing more about this language in future.

No comments:


Copyright © 2016 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.