Is the python import object automatically if it is created by another object?

I am learning module, and feel a little confused with “from datetime import datetime”. I think this is import a type of datetime from the module of datetime.

When I use this expression:
df = datetime.now()-datetime(2023,6,25)

The df is “datetime.timedelta()”. But I didn’t import timedelta from the module of datetime, so is it imported automatically?

1 Like

What’s the link to the lesson?

You’re importing datetime from the datetime module.
It includes: year, month, day, hour, minute, second, microsecond, & tzinfo.
.timedelta is an available type of datetime class.

Here’s the documentation on the module:

Thank you for your reply!

The link is: https://www.codecademy.com/courses/learn-python-3/videos/learn-python3-datetimes

I’m confused about the terms which are module, type and class, when I was learning this section and reading your answer.

I had forgotten that this video was in the python3 course. :woman_facepalming: Seems like it’s better suited in a data course where one would have to clean date data to analyze it but I get why it’s here, b/c you’d use this module in programming and it’s an example of an intro to python modules. I’m going to re-watch it as I reply here…so, there will be edits.

It all can be confusing for sure. Reading documentation can add to the confusion, but, I still think it’s the best way to learn (& by seeing examples of the terminology & then writing code too.)

I think that the term “module” and “library” are used kind of interchangeably. I guess, think of a python module like a library that contains a number of built in functions. Like in this case, datetime.
Official docs on modules:

In the video, he imported the datetime class from the datetime module. Think of a class like a sort of template for creating an object. All instances are objects of a class that share the same data structures… which will tell you what type it is. (you can read up more on OOP & Python as well). You can import an entire module/library with all the “parts” or, just specify what you need. Like with the math module. You wouldn’t necessarily want to import all of the functions, but rather just a few to suit what you want to do. So, he’s using datetime here to create an object which is accessible via the variable birthday.
And from that, he can access different parts of the arguments he passed through the function.
birthday.year, birthday.month, etc.

the datetime class has a number of types built into it: date, datetime, timezone. Here’s some examples of its usage. (Data) type will tell you what sort of operations can be performed on the data–numeric (integers, floats), sequence (list, tuple), and text (strings), mappings (dictionaries), etc. All explained here.

2 Likes

Thank you for your answer, but I still have a question, like in this code:

from datetime import datetime df = datetime.now()-datetime(2023,6,25) print(type(df)) print(df.total_seconds())

In this code, I only import datetime.datetime class, I didn’t import datetime.timedelta class, why can I still use datetime.timedelta like “df.total_seconds()”?

Because timedelta() is an available type the class datetime which is part of the datetime library.

Check out the available types linked above and here:

You can use the built-in function dir() to see the information about the class you’ve imported including its attributes.:

ex:

>>> from datetime import datetime
>>> dir(datetime)
['__add__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', 
'__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', 
'__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', 
'__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__',
 'astimezone', 'combine', 'ctime', 'date', 'day', 'dst', 'fold', 'fromisocalendar', 'fromisoformat',
 'fromordinal', 'fromtimestamp', 'hour', 'isocalendar', 'isoformat', 'isoweekday', 'max', 
'microsecond', 'min', 'minute', 'month', 'now', 'replace', 'resolution', 
'second', 'strftime', 'strptime', 'time', 'timestamp', 'timetuple', 'timetz', 
'today', 'toordinal', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow', 
'utcoffset', 'utctimetuple', 'weekday', 'year']

Sorry, my English is not very good, I think this sentence have grammar problem, I couldn’t understand it’s meaning. Could you rephrase it?

There is no “timedelta” in the result above.

I’m sorry for the confusion.

timedelta is an available class (one of six available) of the module datetime (automatically imported). It’s also an available type. (which as I write this, is confusing). See also here.

And,
https://docs.python.org/3/library/datetime.html#timedelta-objects

which is why you’re able to calculate the difference between seconds.

Maybe this explanation will break it down better than the documentation.

Sorry, I think I didn’t express my question clearly in this post. I rephrased my question in this post: Why can I use timedelta class's function even though I haven't import timedelta class? .

Okay, but please don’t post the same question more than once. You’ve already posted this above.

When you imported the datetime class from the datetime module, that included the timedelta type, which is, for lack of a better word, part of the datetime class. that’s why you were able to perform this function:

df = datetime.now()-datetime(2023,6,25)

<class 'datetime.timedelta'>
404893.775742

You don’t have to import timedelta outright b/c it’s already included in the import.

I hope that helps.