无法访问继承的 Python 类中的父属性

Mel*_*art 1 python inheritance

这是我的父类,

class BaseResource:
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        fmt = '[%(asctime)s] [%(levelname)s] [%(message)s] [--> %(pathname)s [%(process)d]:]'
        logging.basicConfig(format=fmt, level=logging.DEBUG)

    def log(self, msg):
        self.logger.debug(msg)
Run Code Online (Sandbox Code Playgroud)

这是我继承的对象,

class SendOTP(BaseResource):

    def __init__(self):
        super(BaseResource, self).__init__()

    def on_post(self, req, res):
        self.logger.log("[FAILURE]..unable to read from POST data")
Run Code Online (Sandbox Code Playgroud)

这会引发以下错误,

AttributeError: 'SendOTP' object has no attribute 'logger'
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么。

use*_*ica 5

应该是super(SendOTP, self),不是super(BaseResource, self)

另外,如果这是 Python 3,您可以将其简化为super(); 如果是 Python 2,则还需要将声明更改BaseResource

class BaseResource(object):
Run Code Online (Sandbox Code Playgroud)

得到一个新型的类。