Skip to content Skip to sidebar Skip to footer

Import Class Defined In Same Module File?

I have a module file called mymodule.py, which contains the following code: class foo: def __init__(self): self.foo = 1 class bar: import foo def __init__(self

Solution 1:

Classes are imported with module name first. However, you don't need to import classes in mymodule from within mymodule, just use it. Meaning: remove the import foo line

Solution 2:

You do not need to import an object defined in the same module:

classfoo:def__init__(self):
        self.foo = 1classbar:def__init__(self):
        self.bar = foo().foo

The import statement is intended for objects defined in other files only; you import the names defined in another python file into the current module.

Post a Comment for "Import Class Defined In Same Module File?"