无法在python 2.7中获取抽象类

时间:2016-11-17 00:17:50

标签: python abstract-class

对Python来说真的很新。我有一个抽象基类BankAccount,有两个未实现的方法:存入和取出。

我创建了两个子类SavingsAccount和CurrentAccount。这两个子类旨在为方法提供实现:从抽象类继承的存入和撤销。

它在我的机器上工作得很好,但我一直收到这个错误:

from abc import ABCMeta, abstractmethod
                    ^
SyntaxError: invalid syntax

在线上平台上,意图呈现。

这是我的代码:


from abc import ABCMeta, abstractmethod

class BankAccount:
    __metaclass__ = ABCMeta
    def __init__(self):pass
    @abstractmethod
    def deposit(self, amount):pass
    @abstractmethod
    def withdraw(self, amount):pass

class SavingsAccount(BankAccount):
    def __init__(self):
        self.balance = 500

    def deposit(self, amount):
        if 0 > amount:
            return "Invalid deposit amount"
        self.balance += amount
        return self.balance

    def withdraw(self, amount):
        if 0 > amount:
            return "Invalid withdraw amount"
        elif self.balance - amount  self.balance:
            return "Cannot withdraw beyond the current account balance"
        self.balance -= amount
        return self.balance


class CurrentAccount(BankAccount):
    def __init__(self):
        self.balance = 0

    def deposit(self, amount):
        if 0 > amount:
            return "Invalid deposit amount"
        self.balance += amount
        return self.balance

    def withdraw(self, amount):
        if 0 > amount:
            return "Invalid withdraw amount"
        elif amount > self.balance:
            return "Cannot withdraw beyond the current account balance"
        self.balance -= amount
        return self.balance

已更新:完整错误日志



Traceback (most recent call last):
  File "python/nose2/bin/nose2", line 8, in 
    discover()
  File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 300, in discover
    return main(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 100, in __init__
    super(PluggableTestProgram, self).__init__(**kw)
  File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 133, in parseArgs
    self.createTests()
  File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 258, in createTests
    self.testNames, self.module)
  File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/loader.py", line 67, in loadTestsFromNames
    for name in event.names]
  File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/loader.py", line 82, in loadTestsFromName
    result = self.session.hooks.loadTestsFromName(event)
  File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/events.py", line 224, in __call__
    result = getattr(plugin, self.method)(event)
  File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/plugins/loader/testclasses.py", line 119, in loadTestsFromName
    result = util.test_from_name(name, module)
  File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/util.py", line 106, in test_from_name
    parent, obj = object_from_name(name, module)
  File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/util.py", line 117, in object_from_name
    module = __import__('.'.join(parts_copy))
  File "/home/ubuntu/Applications/andelabs-server/tmp/57d7029af597c64100ba88a5-57bc46aae713b01900df735e-test.py", line 4, in 
    from tmp.andelabs_57d7029af597c64100ba88a5_57bc46aae713b01900df735e import *
  File "/home/ubuntu/Applications/andelabs-server/tmp/andelabs_57d7029af597c64100ba88a5_57bc46aae713b01900df735e.py", line 1
    from abc _import_ ABCMeta, abstractmethod
                    ^
SyntaxError: invalid syntax


0 个答案:

没有答案