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.
- Seven steps to compile Python 3.5.0 from source
- Getting started with Python and Anjuta
- Starting up with Python – Part 1 – Numbers and Maths, Variables
- Starting up with Python – Part 2 – Modules and Functions, Strings
- Starting up with Python – Part 3 – Sequences and List, Slicing and Editing
- Starting up with Python – Part 4 – More list functions and Slicing lists
- Starting up with Python – Part 5 – Methods
- Starting up with Python – Part 6 – Sort and Tuples, Strings and stuffs, methods and Dictionary
- Starting up with Python – Part 7 – If statement, Else and elif statements
- Starting up with Python – Part 8 – Nesting and Comparison operators
- Starting up with Python – Part 9 – And and Or operators
- Starting up with Python – Part 10 – While, For, Infinite and Break Loops
- Starting up with Python – Part 11 – Building functions and Default parameters
- Starting up with Python – Part 12 – Multiple Parameters, Parameter Types and Tuples as Parameters
- Starting up with Python – Part 13 – Object Oriented Program and Classes and Self
- Starting up with Python – Part 14 – Subclasses, Overwrite variable on Sub, Multiple Parent classes
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.