3

This is my parent class,

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)

This is my inherited object,

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")

This throws up the following error,

AttributeError: 'SendOTP' object has no attribute 'logger'

What am I doing wrong here.

3
  • Can't reproduce: ideone.com/Xmp1b2 Commented Jan 6, 2018 at 17:21
  • 2
    If this is python3, you could simplify the super call to super().__init__() Commented Jan 6, 2018 at 17:22
  • @melpomene imho, call on_post Commented Jan 6, 2018 at 17:23

3 Answers 3

7

It should be super(SendOTP, self), not super(BaseResource, self).

Also, if this is Python 3, you can simplify it to just super(); if Python 2, you also need to change the declaration of BaseResource to

class BaseResource(object):

to get a new-style class.

Sign up to request clarification or add additional context in comments.

4 Comments

This worked but just curious, class BaseResource(object) is the old Python 2 style, right?
But now I get the following error, TypeError: log() missing 1 required positional argument: 'msg'
So I guess this is an additional question, I'm trying to call a method from the super and I'm passing in the correct argument and still getting an error.
2

If it is python 3 you have to use: Super()._init_()

instead of super(BaseResource, self).__init__()

and

super().log("[FAILURE]..unable to read")

instead of self.logger.log("[FAILURE]..unable to read")

If it is python 2 you have to use: BaseResource.__init__(self)

instead of super(BaseResource, self).__init__()

and

self.log("[FAILURE]..unable to read")

instead of self.logger.log("[FAILURE]..unable to read")

Comments

1

In super you should put your class type

>>> class A(object):
...     def __init__(self):
...             print("hi")

>>> class B(A):
...     def __init__(self):
...             super(B, self).__init__()

>>> class C(A):
...     def __init__(self):
...             super(A, self).__init__()

>>> B()
hi
<__main__.B object at 0x0000024F90141C88>

>>> C()
<__main__.C object at 0x0000024F90141D30>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.