-2

enter image description here

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...

quamrana
  • 37,849
  • 12
  • 53
  • 71
JeongHaSeung
  • 393
  • 6
  • 12

2 Answers2

1

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

quamrana
  • 37,849
  • 12
  • 53
  • 71
0

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.

Also you can check this and this to know more.

alyferryhalo
  • 43
  • 1
  • 12