3-1-5-24. Techniques for Importing Modules
There are some other variance of importing that are useful in other situations.
You can import an individual function or class from a module like this,
from, the module name,
import, the object name.
This gives access only to defaultdict from the collections module.
Defaultdict will be accessible with its own name without the module name before it.
Trying to access collections or even calling
collections.defaultdict will give a name error.
Importing individual objects from a module means you only take
what you need and you don’t need to use.notation to access them.
You can import multiple individual objects from a module by separating them with commas.
This technique is very common when importing pieces from large libraries.
And as you saw before,
you can import a module and give it a different,
usually shorter name, like this.
If the name of a module is particularly long or
if there’s a clash with something with the same or similar name,
renaming the module like this can be helpful.
Check code examples in the documentation as these will often
include a standard abbreviation if one is used for this module.
Using an abbreviation that is consistent with others will make your code more readable.
For example, the standard abbreviation for the multiprocessing module is MP.
You can combine the previous two pieces of syntax to
input an item from a module and change its name.
Again, you’ll be able to access
only that individual item directly with its newly specified name.
No.notation is needed.
This can be useful if you have multiple objects
with similar names from different packages in your namespace.
For example, perhaps you want a CSV reader and JSON reader.
You could import them from their respective modules and give them descriptive names.
Another way of importing that you may see in
other people’s code but that you should not use is this.
Using this asterisk will import every object from
a module individually and allow you to access each of them directly via its name.
The real problem with this is that modules may contain many objects,
each of which has a name.
Including all these names may overwrite or maybe
overwritten by other names you’re using in your program.
Import star also makes it impossible for
collaborators to find where an important object was defined.
A reader can search for a definition of a function and not find it and
they won’t know which import star statement introduced a function.
These problems can lead to a lot of confusion.
Do not use from module name import asterisk.
If you really want to use all the objects from the Random module,
use the standard import random instead and access each of the objects with the.notation.
지금까지 import 다음에 모듈 이름이 오는 모듈을 가져왔습니다.
객체의 모든 클래스를 만들고
해당 모듈의 기능은.notation을 통해 사용할 수 있습니다.
다른 상황에서 유용한 가져오기의 다른 변형이 있습니다.
다음과 같이 모듈에서 개별 함수 또는 클래스를 가져올 수 있습니다.
from, 모듈 이름,
가져오기, 개체 이름.
이렇게 하면 컬렉션 모듈에서 defaultdict에만 액세스할 수 있습니다.
Defaultdict는 앞에 모듈 이름 없이 고유한 이름으로 액세스할 수 있습니다.
컬렉션에 액세스하거나 전화를 걸거나
collections.defaultdict는 이름 오류를 제공합니다.
모듈에서 개별 개체를 가져오는 것은
필요한 것과 액세스하기 위해 .notation을 사용할 필요가 없습니다.
쉼표로 구분하여 모듈에서 여러 개별 개체를 가져올 수 있습니다.
이 기술은 대규모 라이브러리에서 조각을 가져올 때 매우 일반적입니다.
그리고 전에 보셨다시피,
모듈을 가져와서 다르게 지정할 수 있습니다.
일반적으로 이와 같이 더 짧은 이름.
모듈 이름이 특히 길거나
동일하거나 유사한 이름을 가진 항목과 충돌이 있는 경우
이와 같이 모듈의 이름을 바꾸는 것이 도움이 될 수 있습니다.
문서에서 코드 예제를 확인하십시오.
이 모듈에 사용되는 경우 표준 약어를 포함합니다.
다른 사람들과 일관된 약어를 사용하면 코드를 더 읽기 쉽게 만들 수 있습니다.
예를 들어, 다중 처리 모듈의 표준 약어는 MP입니다.
이전 두 구문을 결합하여
모듈에서 항목을 입력하고 이름을 변경합니다.
다시, 당신은 액세스 할 수 있습니다
새로 지정된 이름으로 직접 해당 개별 항목만.
No.표기가 필요합니다.
여러 개체가 있는 경우 유용할 수 있습니다.
네임스페이스의 다른 패키지에서 유사한 이름을 사용합니다.
예를 들어 CSV 리더와 JSON 리더가 필요할 수 있습니다.
각각의 모듈에서 가져와서 설명적인 이름을 지정할 수 있습니다.
에서 볼 수 있는 또 다른 가져오기 방법
다른 사람의 코드이지만 사용하지 말아야 할 것은 이것이다.
이 별표를 사용하면
모듈을 개별적으로 사용하고 이름을 통해 각 모듈에 직접 액세스할 수 있습니다.
이것의 진짜 문제는 모듈에 많은 객체가 포함될 수 있다는 것입니다.
각각 이름이 있습니다.
이 모든 이름을 포함하면 덮어쓰거나
프로그램에서 사용 중인 다른 이름으로 덮어씁니다.
수입 별은 또한 불가능합니다.
공동 작업자는 중요한 개체가 정의된 위치를 찾습니다.
독자는 함수의 정의를 검색하고 찾지 못할 수 있습니다.
그들은 어떤 import star 문이 기능을 도입했는지 알지 못할 것입니다.
이러한 문제는 많은 혼란을 초래할 수 있습니다.
from 모듈 이름 import 별표를 사용하지 마십시오.
Random 모듈의 모든 객체를 정말로 사용하고 싶다면,
대신 표준 import random을 사용하고 .notation으로 각 객체에 액세스하십시오.
However, this syntax for importing will only work for sub-modules.
You cannot import a function from a module in this way.
If you want to use other parts of the OS module two,
you could import OS instead and everything in the os.path will still be accessible.
Sometimes, naming can be a point of confusion when working with modules.
For example, a module might be named after
one of the important classes or functions within it.
In this case, you will need to think carefully about your import statements.
This imports the datetime class from the datetime module.
Note that after this, using datetime,
will refer to the datetime class, not the module.
Python 표준 라이브러리의 일부 모듈에는 많은 내용이 있습니다.
코드를 더 잘 관리하려면
패키지에 포함된 하위 모듈로 나뉩니다.
패키지는 단순히 하위 모듈을 포함하는 모듈입니다.
하위 모듈은 일반적인 점 표기법으로 지정됩니다.
예를 들어 OS 모듈,
파일 시스템을 처리하는 데 유용한 하위 모듈 os.path가 있습니다.
특히 경로 이름을 처리하는 데 사용됩니다.
하위 모듈인 모듈은 다음과 같이 지정됩니다.
패키지 이름과 점으로 구분된 하위 모듈 이름.
다음과 같이 하위 모듈 os.path를 가져올 수 있습니다.
그런 다음 일반적인 방법으로 하위 모듈의 개체를 사용할 수 있습니다.
그러나 가져오기를 위한 이 구문은 하위 모듈에서만 작동합니다.
이런 식으로 모듈에서 함수를 가져올 수 없습니다.
OS 모듈 2의 다른 부분을 사용하려면,
대신 OS를 가져올 수 있으며 os.path의 모든 항목에 계속 액세스할 수 있습니다.
때로는 모듈로 작업할 때 이름 지정이 혼동을 줄 수 있습니다.
예를 들어 모듈의 이름은
그 안의 중요한 클래스 또는 기능 중 하나입니다.
이 경우 수입 명세서에 대해 신중하게 생각해야 합니다.
이것은 datetime 모듈에서 datetime 클래스를 가져옵니다.
이 후에 datetime을 사용하여
모듈이 아닌 datetime 클래스를 참조합니다.
Techniques for Importing Modules
There are other variants of import
statements that are useful in different situations.
- To import an individual function or class from a module:
from module_name import object_name
- To import multiple individual objects from a module:
from module_name import first_object, second_object
- To rename a module:
import module_name as new_name
- To import an object from a module and rename it:
from module_name import object_name as new_name
- To import every object individually from a module (DO NOT DO THIS):
from module_name import *
- If you really want to use all of the objects from a module, use the standard import module_name statement instead and access each of the objects with the dot notation.
import module_name
Modules, Packages, and Names
In order to manage the code better, modules in the Python Standard Library are split down into sub-modules that are contained within a package. A package is simply a module that contains sub-modules. A sub-module is specified with the usual dot notation.
Modules that are submodules are specified by the package name and then the submodule name separated by a dot. You can import the submodule like this.
import package_name.submodule_name
댓글을 달려면 로그인해야 합니다.