I wrote this code
What is the difference between import contextlib
and from contextlib import contextmanager
in the link:
https://docs.python.org/3/library/contextlib.html
I'm so confused...
I wrote this code
What is the difference between import contextlib
and from contextlib import contextmanager
in the link:
https://docs.python.org/3/library/contextlib.html
I'm so confused...
What is the difference between "import contextlib" and "from contextlib import contextmanager"
In a way there is no difference: the last name is the name of a variable that python creates for you which it has assigned to something in a different python module.
In the first case: import contextlib
, you get the variable contextlib
which happens to be a module and to access anything from it you have to use that name followed by a .
: @contextlib.contextmanager
In the second case: from contextlib import contextmanager
, you get the variable contextmanager
which happens to be a class that you can access directly by using its name: @contextmanager
There are a lot of information in the Internet. I suppose that you can google more proper and deeper information to answer your question by your own than you can receive here.
For example, there is a good question about difference between import module
and from module import something
in Python. You can read it here.