Rohit
Rohit

Reputation: 33

Python modules usage

I was writing a code in python and got stuck with a doubt. Seems irrelevant but can't get over it. The thing is when I import a module and use it as below:

import math
print math.sqrt(9)

Here I see math(module) as a class which had a method sqrt(). If that is the case then how can I directly use the class without creating an object of it. I am basically unable to understand here the abstraction between class and and object.

Upvotes: 0

Views: 51

Answers (4)

GIZ
GIZ

Reputation: 4643

Here I see math(module) as a class which had a method sqrt(). If that is the case then how can I directly use the class without creating an object of it. I am basically unable to understand here the abstraction between class and and object.

When you import a module, the module object is created. Just like when you use open('file.txt') a file object will be created.

You can use a class without creating an object from it by referencing the class name:

class A:
    value = 2 + 2 
A.value 

class A is an object of class type--the built-in class used to create classes. Everything in Python is an object.

When you call the class A() that's how you create an object. *Sometimes objects are created by statements like import creates a module object, def creates a function object, classcreates a class object that creates other objects and many other statements...

Upvotes: 0

d-coder
d-coder

Reputation: 13993

AFAIK all python modules (like math and million more) are instantiated when you have imported it. How many times are they instantiated you ask ? Just once! All modules are singletons.

Just saying the above statement isn't enough so let's dive deep into it. Create a python module ( module is basically any file ending with ".py" extension ) say "p.py" containing some code as follows:

In p.py

print "Instantiating p.py module. Please wait..."
# your good pythonic optimized functions, classes goes here
print "Instantiating of p.py module is complete."

and in q.py try importing it

import p

and when you run q.py you will see..

Instantiating p.py module. Please wait...
Instantiating of p.py module is complete.

Now have you created an instance of it ? NO! But still you have it up and running ready to be used.

Upvotes: 1

Kos
Kos

Reputation: 72299

Modules are more like objects, not like classes. You don't "instantiate" a module, there's only one instance of each module and you can access it using the import statement.

Specifically, modules are objects of type 'module':

>>> import math
>>> type(math)
<type 'module'>

Each module is going to have a different set of variables and methods.

Modules are instantiated by Python, whenever they are first imported. Modules that have been instantiated are stored in sys.modules:

>>> import sys
>>> 'math' in sys.modules
False
>>> import math
>>> 'math' in sys.modules
True
>>> sys.modules['math'] is math
True

Upvotes: 1

magicleon94
magicleon94

Reputation: 5192

In your case math is not a class. When you import math the whole module math is imported. You can see it like the inclusion of a library (the concept of it).
If you want to avoid to import the whole module (in order to not have everything included in your program), you can do something like this:

from math import sqrt
print sqrt(9)

This way only sqrt is imported and not everything from the math module.

Upvotes: 0

Related Questions