इस रूप में की उम्मीदमैं फ़ंक्शन में `आयात *` का उपयोग क्यों नहीं कर सकता?
def outer_func():
from time import *
print time()
outer_func()
मैं संदर्भ ठीक में नेस्टेड कार्यों को परिभाषित और उन्हें अन्य नेस्टेड कार्यों से कॉल कर सकते हैं काम करता है:
def outer_func():
from time import time
def inner_func():
print time()
inner_func()
outer_func()
:
def outer_func():
def time():
return '123456'
def inner_func():
print time()
inner_func()
outer_func()
मैं भी व्यक्तिगत कार्यों आयात कर सकते हैं
हालांकि, SyntaxError: import * is not allowed in function 'outer_func' because it contains a nested function with free variables
फेंकता है:
def outer_func():
from time import *
def inner_func():
print time()
inner_func()
outer_func()
मुझे पता है कि यह सबसे अच्छा अभ्यास नहीं है, लेकिन यह क्यों काम नहीं करता है?
दिलचस्प सवाल ... – mgilson
"यह अपेक्षित काम करता है" - जिसमें पाइथन संस्करण है? – georg
@ thg435 पायथन 2. पायथन 3 कठोर है और 'सिंटेक्स त्रुटि: आयात * केवल मॉड्यूल स्तर पर अनुमत' के साथ पहला उदाहरण मना कर देता है। मैंने स्पष्टीकरण के लिए पायथन-2.x टैग जोड़ा है। –