2

In first.py, I imported the datetime library and called a method which is written in second.py. The method works well without importing the datetime libirary in second.py.

first.py

from datetime import datetime
import second

def method1(time):
    return datetime.strptime(time,"%Y/%m/%d")

a = method1("2019/08/01")
b = second.method2(a)

second.py

def method2(para1):
    return para1.second

Output

0

Should second.py import datetime so that para1.second can work? Can someone help explain the rationale behind?

1
  • Note that you are actually running the code from within first.py. the method being executed from there and there the library is known. When you import you get the namespace and so both method2 and datetime are defined in first.py. Also note that in second.py there is no need to be "aware" of datetime as it is not explicitly used, so no error there Commented Jul 8, 2019 at 14:14

2 Answers 2

2

You only need to import modules explicitly when you need to use their names. In first.py, for example, you're using things in the datetime module directly, and referring to it by name. So, you do import datetime, and then call datetime.strptime() on that module you imported.

In second.py, however, you don't have to do import datetime. This is because of how python handles attributes - when you do para1.second, python doesn't need to know exactly what type of variable para1 is - it just checks to see whether it has a field called second. And it does, so it returns that. Nowhere in second.py are you referring to datetime directly - only indirectly, via a variable that was defined from it.

Also consider that the datetime module does a lot of stuff on its own, and almost certainly imports other dependencies that you're not aware of and you're not importing yourself. But you can still use the datetime module, because you don't need to explicitly refer to those modules it's using behind the scenes. They're still in memory somewhere, and if you call certain methods from datetime, that code will still get executed, but you don't need to be directly aware of it.

Sign up to request clarification or add additional context in comments.

Comments

1

Python usually uses duck typing1. This means that instead of requiring a particular type for an object, it looks at the actual attributes it has.

What this means in your case is that method2 does not care in the slightest whether you pass in a datetime object or not. All that's required is that the input para1 have a second attribute.

Importing datetime into second.py would be counter-productive. It wouldn't affect the operation of your method in any way, but it would polute your namespace and set up an implication that isn't necessarily true.


1 A notable counterexample is a sum of strings, e.g. sum(['a', 'b'], ''). Aside from that, your own code can choose what to do as you see fit if course.

2 Comments

thanks! that's clear. i know it's rather simplified example but just wondering is it normal to construct the code in that way?
@Nathan. I was hoping to convey that it absolutely is normal. You only import the names that you need. A module is just an object with some attributes on it, just like any other object. You shouldn't import it if you don't need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.