Home > Software engineering >  Inherit parent class attributes, __init__, etc
Inherit parent class attributes, __init__, etc

Time:01-31

I am trying to use a parent class as a blueprint for new classes.

E.g. the FileValidator contains all generic attributesand methods for a generic file. Then I want to create for example a ImageValidator inheriting everything from the FileValidator but with additional, more specific attribtues, methods. etc. In this example the child class is called: FileValidatorPlus

My understanding was, that if I inherit the parent class I can just plug-in more attributes/methods without repeating anything, like just adding min_size. But the following code gives: TypeError: FileValidatorPlus.__init__() got an unexpected keyword argument 'max_size'

class File:
    def __init__(self, file_size=100):
        self.file_size = file_size


class FileValidator(object):
    error_messages = {'max_size': 'some error msg'}

    def __init__(self, max_size=None):
        self.max_size = max_size

    def __call__(self, file):
        print(self.max_size > file.file_size)


class FileValidatorPlus(FileValidator):
    error_messages = {'min_size': 'some other error msg'}

    def __init__(self, min_size=None):
        super(FileValidatorPlus, self).__init__()
        self.error_messages.update(super(FileValidatorPlus, self).error_messages)

        self.min_size = min_size


validator = FileValidatorPlus(max_size=10, min_size=20)
print(validator.error_messages)
print(validator.max_size)
print(validator.min_size)

Full Traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
TypeError: FileValidatorPlus.__init__() got an unexpected keyword argument 'max_size'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/opt/.pycharm_helpers/pydev/_pydev_bundle/pydev_code_executor.py", line 108, in add_exec
    more, exception_occurred = self.do_add_exec(code_fragment)
  File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 90, in do_add_exec
    command.run()
  File "/opt/.pycharm_helpers/pydev/_pydev_bundle/pydev_console_types.py", line 35, in run
    self.more = self.interpreter.runsource(text, '<input>', symbol)
  File "/usr/local/lib/python3.10/code.py", line 74, in runsource
    self.runcode(code)
  File "/usr/local/lib/python3.10/code.py", line 94, in runcode
    self.showtraceback()
  File "/usr/local/lib/python3.10/code.py", line 148, in showtraceback
    sys.excepthook(ei[0], ei[1], last_tb)
  File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 112, in info
    traceback.print_exception(type, value, tb)
NameError: name 'traceback' is not defined
Traceback (most recent call last):
  File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 284, in process_exec_queue
    interpreter.add_exec(code_fragment)
  File "/opt/.pycharm_helpers/pydev/_pydev_bundle/pydev_code_executor.py", line 132, in add_exec
    return more, exception_occurred
UnboundLocalError: local variable 'exception_occurred' referenced before assignment
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 511, in <module>
    pydevconsole.start_server(port)
  File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 407, in start_server
    process_exec_queue(interpreter)
  File "/opt/.pycharm_helpers/pydev/pydevconsole.py", line 292, in process_exec_queue
    traceback.print_exception(type, value, tb, file=sys.__stderr__)
UnboundLocalError: local variable 'traceback' referenced before assignment
Process finished with exit code 1

CodePudding user response:

You need to add max_size as a parameter in your FileValidatorPlus constructor.

If you want FileValidatorPlus to inherit this field from the super's constructor, you must not define a __init__ method.

In other words, if you want FileValidatorPlus to inherit max_size parameter from FileValidator, you should delete __init__ because the subclass only inherits constructor properties when they don't have their own constructor.

But in your case I think that it would be a better solution just to add min_size=None to FileValidatorPlus.__init__:

    def __init__(self, max_size=None, min_size=None):
        super(FileValidatorPlus, self).__init__(max_size)

CodePudding user response:

I got also different error :

TypeError: __init__() got an unexpected keyword argument 'max_size'

and the solution would be :

    def __init__(self, max_size=None, min_size=None):
        super(FileValidatorPlus, self).__init__(max_size)

Output:

{'min_size': 'some other error msg', 'max_size': 'some error msg'} 
10
20
  •  Tags:  
  • Related