类继承抽象类时导入包模块

原学程将引见类继续笼统类时导进包模块的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

类继承抽象类时导入包模块 教程 第1张

成绩描写

bank
 __init__.py
 Account.py
 SavingAccount.py
main.py

SavingAccount类继续自Account(笼统类)。当main.py导进SavingAccount时以下所示:

去自银言。SavingAccount导进SavingAccount

它会显示"出著名为‘Account’的模块"。有甚么人晓得怎样处理它?

输入窗心中的完全毛病代码以下:

Traceback (most recent call last):
  File "main.py", line 五, in <module>
 from bank.SavingAccount import SavingAccount
  File "ankSavingAccount.py", line 一, in <module>
 from Account import Account
ModuleNotFoundError: No module named 'Account'

Acccount t.py

from abc import ABCMeta,abstractmethod
class Account(metaclass=ABCMeta):
 _id = 0
 _name = ''
 _balance = 0
 __next = 0

 def __init__(self,name,initBal = 一000):
  self._name=name;
  self._balance = initBal

SavingAccount t.py

from Account import Account
class SavingAccount(Account):
 _interestRate = 0

 def __init__(self,name,initBal=0):
  super(SavingAccount,self).__init__(name,initBal)

 @classmethod
 def interestRate(cls):
  _interestRate = 0

 @classmethod
 def interestRate(cls,rate):
  cls._interestRate = rate

推举谜底

您应当变动

from Account import Account

from .Account import Account

修议在包内应用后1种绝对导进办法。

佳了闭于类继续笼统类时导进包模块的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。