I have the following class in python:
class Foo():
def __init__(self):
self.id=8
self.value=self.get_value() #Mark-1
def get_value(self):
pass
def should_destroy_earth(self):
return self.id ==42
class Baz(Foo):
def get_value(self,some_new_parameter):
pass
Here, i can create an object of class Foo as such:
ob1 = Foo() # Success
However, if i try to create an object of Baz, it throws me an error:
Ob2 = Baz() # Error
File "classesPython.py", line 20, in <module>
ob2 = Baz()
File "classesPython.py", line 4, in __init__
self.value=self.get_value()
TypeError: get_value() takes exactly 2 arguments (1 given)
If i remove the "self.get_value" in 3rd line of class Foo, i can create an object of class Baz.
Isn't is the case that when we inherit a class in python, the init method of the superclass isn't inherited by the subclass and we need to explicitly call them (e.g. Superclass.init(self) )?
So when i create an object of Baz, the __init__method of the Foo() class isn't invoked by default. So why can i not create the object for Baz ?
Bazdoesn't actually support the same interface asFoo, and therefore shouldn't be a subclass of it? Could you give a less abstract example?