Python Module से Single Function Import करना
आप "from module import function" Statement का उपयोग करके Python Module से Specific Function Import कर सकते हैं।
उदाहरण
यदि आप math Library से sin Function Import करना चाहते हैं बिना किसी अन्य Function को Import किए:
>>> from math import sin >>> sin(0) 0.0
ध्यान दें कि आपको sin को "math." से Prefix नहीं करना पड़ता क्योंकि केवल sin Import किया गया है, math नहीं।
Imported Functions को Alias करना
आप Imported Functions को Alias भी कर सकते हैं:
>>> from math import cos as cosine >>> cosine(0) 1.0
उपयोगी Patterns:
# Multiple Specific Imports
from os.path import join, exists, basename, dirname
# With Aliases (Name Conflicts Avoid करने के लिए)
from json import loads as json_loads, dumps as json_dumps
from yaml import load as yaml_load, dump as yaml_dump
# Conditional Import (Optional Dependencies के लिए)
try:
import ujson as json
except ImportError:
import json