Reputation: 571
I am a newbie, and picking up Python (3.4) and trying to work on classes.
I have created a module BootstrapYieldCurve, with two classes (BootstrapYieldCurve and ForwardRates) inside.
When I import the module, I can call and manipulate the BootstrapYieldCurve class, but I get an error from calling the ForwardRates class.
Would someone be able to advise if the code is wrong, or the way I call the class in Python Shell?
>>> from BootstrapYieldCurve import ForwardRates
>>> Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
from BootstrapYieldCurve import ForwardRates
ImportError: cannot import name 'ForwardRates'
import math
class BootstrapYieldCurve():
def __init__(self):
def add_instrument(..........):
..........
class ForwardRates():
def .........
Upvotes: 1
Views: 1312
Reputation: 2233
You cannot import one class from another class like this :
from BootstrapYieldCurve import ForwardRates
because ForwardRates
is not encapsulated inside BootstrapYieldCurve
(Also, there is no such concept in Python). See encapsulation.
You can import variables, functions, and classes
from a module
(a python file that generally have variables, functions, and classes)
You can do something like :
from module_file import BootstrapYieldCurve
and
from module_file import ForwardRates
You can refer to this basic tutorial on importing modules.
Also, try not to name module
and class
with same name
as that may create confusion. It is a not a good practice to do that as module names share the same namespace as everything else.
Upvotes: 1