Date and time, python beginner tutorial


#1



Hi guys, I am new here and I have a question to ask about the date and time exercise of python given below in the link.

https://www.codecademy.com/courses/python-beginner-en-zFPOx/0/2?curriculum_id=4f89dab3d788890003000096#_=_

Now my question is what is the meant by writing "from" and then writing "import" in the first line? I'm a java programmer (high school student so I have still a lot to learn in java :stuck_out_tongue: ) so I am used to the concept of importing built in classes but in here it is confusing me, Please help me clear this confusion. Thanks guys :smiley:

Regards,
Arkaprabha



#2

you can import a whole library:
import datetime
or a specific function from a library:
from datetime import datetime


#3

Names in Python refer to objects (functions, classes, types, it's all objects)
You can create a name in your namespace (module) that refers to something defined somewhere else.

When you import a module, that file is executed (which creates the things it defines) and then you can use them.


#4

Hi Stetim94,

So you mean that "import" is used for importing the library as a whole and in the second one, we are just importing one of library's function right?
I thought that datetime is the library but now I realised that datetime is the library and one of its function is also named datetime.. I think that it is kind of awkward of the python developers that they used the same name for defining the library and its function,make things more confusing.


#5

Hey ionatan,

Can you explain me more clearly on what you are saying, I didn't get it. Especially the "namespace" part you mentioned. Thanks! :smiley:


#6

The space where a name is defined. Scope. For example, a function has its own namespace.

Import always runs the file that you're importing. But you can create different names in your namespace and make those names refer to different things. You can have a reference to the module or to things inside it.

The datetime in the datetime module is a class.


#7

@ionatan is explaining this in detail, but python handles a very different system of importing functions.


#8

When you import something you run a file. This happens no matter what.
Then names are created to refer to the module you imported, or stuff in it such as a function or a class

Here's datetime.py (latest version, not released): https://hg.python.org/cpython/file/tip/Lib/datetime.py

When you import datetime, this is what you run.
In it, there's a class, datetime. Use your browser's search function "class datetime", that's what you're referring to if you do from datetime import datetime


#9

Oh ok.. I got it. Thanks for helping me out guys. Cheers :smile: