Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing time module twice

Tags:

python

I saw the code from Youtube and have a question.

The code below imports time twice.

  1. import time
  2. from time import mktime

import pandas as pd
import os
import time
from datetime import datetime
from time import mktime

By importing time on third line, I think 5th line is useless.

Why does he import time twice?

like image 712
Kwang Avatar asked Dec 27 '15 07:12

Kwang


People also ask

What happens if I import the same module twice?

If multiple modules imports the same module then angular evaluates it only once (When it encounters the module first time). It follows this condition even the module appears at any level in a hierarchy of imported NgModules.

Can we import a module twice?

The rules are quite simple: the same module is evaluated only once, in other words, the module-level scope is executed just once. If the module, once evaluated, is imported again, it's second evaluation is skipped and the resolved already exports are used.

How many times can we import any module in an interpreter session?

For efficiency reasons, each module is only imported once per interpreter session. Therefore, if you change your modules, you must restart the interpreter – or, if it's just one module you want to test interactively, use importlib.reload() , e.g. import importlib; importlib.reload(modulename) .

Does Python import module multiple times?

So each module is imported only one time. To better understand import mechanics I would suggest to create toy example. So import really happens only once. You can adjust this toy example to check cases that are interesting to you.


1 Answers

Why does he import time twice?

He doesn't. He copies time.mktime to mktime. He doesn't have to, he just does so for convenience.

like image 157
Ignacio Vazquez-Abrams Avatar answered Nov 08 '22 02:11

Ignacio Vazquez-Abrams