这是我的父类,
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)
这是我继承的对象,
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")
这会引发以下错误,
AttributeError: 'SendOTP' object has no attribute 'logger'
我在这里做错了什么。
最佳答案
应该是super(SendOTP, self)
,而不是super(BaseResource, self)
。
另外,如果这是 Python 3,您可以将其简化为 super()
;如果是 Python 2,还需要将 BaseResource
的声明改为
class BaseResource(object):
获得一个新式的类(class)。
关于python - 无法访问继承的 Python 类中的父属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48129863/