Starting up with Python – Part15

If you have been following the Python articles since some days, you would have noticed that things are getting more interesting. In case, you have missed the past articles here is a recap. In this article, i will get into Constructors, Import modules, reload modules and Getting modules info.

python_logo_by_bluex_pl

35. Constructors

When an object is created, the methods need to be called but when a contructor is when the first object is created, the objects are automatically called.

Lets see a basic method and object analogy

>> class hackers:
... def mauritius(self):
... print "hackers mauritius"
... 


>> obj=hackers()
>>> obj.mauritius()
hackers mauritius

However, in constructors we want to automatically call the object.

In this example, you would notice a class called new and a method called __init__

>> class new:
... def __init__(self):
... print "hackers mauritius"
... print "this is a constructor"
... 
>>> newobject=new()
hackers mauritius
this is a constructor

36. Import Modules

In python, you can write modules and each time you can call it from anywhere in your code. So let’s create a module.

I created a file called hackers.py with the following simple data.

# cat hackers.py 
def testmod():
 print "this is hackers Mauritius"

This is how you can import different modules imported from a file. Modules can also be imported once per file.

>> import hackers
>>> hackers.testmod()
this is hackers Mauritius

37. Reload modules

However, if the source file is edited and you would import the same module, the result would be the same. To get result from the source file, use this parameter

>> import hackers
>>> hackers.testmod()
this is hackers Mauritius
>>> reload(hackers)
<module 'hackers' from 'hackers.py'>
>>> hackers.testmod()
this is hackers Worldwide

38. Getting modules info

To get modules info, there are in build module. For example, there is an inbuilt module called SQRT

>> import math
>>> math.sqrt(25)
5.0

To know what the module math contains, you can use the dir function

>> dir(math)
['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

There is also the help function. You just need to type this

>> help(math)

You would noticed  a list of what each modules does. However, before using a module you can use this 

>> math.__doc__
'This module is always available. It provides access to the\nmathematical functions defined by the C standard.'

It gives a quick summary of what the module does.

 

 

Nitin J Mutkawoa https://tunnelix.com

Blogger at tunnelix.com | Founding member of cyberstorm.mu | An Aficionado Journey in Opensource & Linux – And now It's a NASDAQ touch!

You May Also Like

More From Author