• 12

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I'm working on a kind of lib, and for a weird reason i have this error.

  • Here is my code. Of course @abc.abstractmethod have to be uncommented
  • Here are my tests

Sorry couldn't just copy and paste it

I went on the basis that the code below works


import abc
import six

class Base(object):

    def whatever(self,):
        raise NotImplementedError

class SubClass(Base):

    def __init__(self,):

        super(Base, self).__init__()

    def whatever(self,):

In the python shell

>>> from test import *
>>> s = SubClass()

Why for my roster module i'm having this error

Can't instantiate abstract class Player with abstract methods _Base__json_builder, _Base__xml_builder

Thanks in advance

Your issue comes because you have defined the abstract methods in your base abstract class with __ (double underscore) prepended. This causes python to do name mangling at the time of definition of the classes.

The names of the function change from __json_builder to _Base__json_builder or __xml_builder to _Base__xml_builder . And this is the name you have to implement/overwrite in your subclass.

To show this behavior in your example -

>>> import abc
>>> import six
>>> @six.add_metaclass(abc.ABCMeta)
... class Base(object):
...     @abc.abstractmethod
...     def __whatever(self):
...             raise NotImplementedError
>>> class SubClass(Base):
...     def __init__(self):
...             super(Base, self).__init__()
...             self.__whatever()
...     def __whatever(self):
...             print("whatever")
>>> a = SubClass()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class SubClass with abstract methods _Base__whatever

When I change the implementation to the following, it works

>>> class SubClass(Base):
...     def __init__(self):
...             super(Base, self).__init__()
...             self._Base__whatever()
...     def _Base__whatever(self):
...             print("whatever")
>>> a = SubClass()

But this is very tedious , you may want to think about if you really want to define your functions with __ (double underscore) . You can read more about name mangling here .

  • 42
Reply Report
    • Am I the only one, who stumbled upon the Error Message? Maybe it's just me, but I'd expect something along these lines: Can't instantiate subclass of abstract class Base without methods some-method. This confused me at first.
      • 1
    • @quapka I think the error message seems fine. The error message indicates that the SubClass class has an 'abstract method' , and its not a concrete method, hence we cannot instantiate an object of it.
      • 1
    • @quapka I agree. The error message Can't instantiate abstract class Foo with abstract methods abstract_method is clearly wrong. The problem is not that an abstract class is being instantiated with a forbidden method, but that a subclass of an abstract class is being instantiated without a required method. It should say something like Can't instantiate subclass Foo of abstract class Bar without method abstract_method_that_must_be_overridden.

Trending Tags