import - Python: importing another .py file -
i have class , want import def function doing:
import <file> but when try call it, says def can not found. tried:
from <file> import <def> but says global name 'x' not defined.
so how can this?
edit:
here example of trying do. in file1.py have:
var = "hi" class a: def __init__(self): self.b() import file2 a() and in file2.py have:
def b(self): print(var) it giving me error though.
import file2 loads module file2 , binds name file2 in current namespace. b file2 available file2.b, not b, isn't recognized method. fix with
from file2 import b which load module , assign b function module name b. wouldn't recommend it, though. import file2 @ top level , define method delegates file2.b, or define mixin superclass can inherit if need use same methods in unrelated classes. importing function use method confusing, , breaks if function you're trying use implemented in c.
Comments
Post a Comment