본문 바로가기

Python

Python_G-009. import vs from ... import vs from... import *

 

Aspect import from ... import from ... import *
Usage Imports the entire module Imports specific functions 
or classes from a module
Imports all functions, classes and
variables into namespace
Example import math from math import sqrt from math import *
Access Requires prefixing
with module name (math.sqrt)
Direct access
to imported elements (sqrt)
Direct access 
without prefix (sqrt)
Pros  - Avoids naming conflicts
 - Clear source of functions
 - Cleaner code
 - Slightly better performance
 - Simplifies code if using many functions
 - Less typing
Cons  - More verbose
 - Slightly slower
 - Limited to specific imports
 - Possible naming conflicts
 - Potential for naming conflicts
 - Less readable
When 
to Use
Best for larger projects to maintain 
clarity and avoid conflicts
When you need 
specific functions or classes
Useful for quick, temporary scripts, or 
when conflicts are unlikely

 

'Python' 카테고리의 다른 글

Python_N-001. basic commend in Numpy  (0) 2025.01.18
Python_G-010. class vs instance  (0) 2025.01.17
Python_G-008. Function  (0) 2025.01.16
Python_G-007. List vs Tuple vs set vs dictionary  (0) 2025.01.16
Python_G-006. 10 Basic Plot (with Matplotlib)  (0) 2025.01.12