Force child class to override parent's methods
up vote
19
down vote
favorite
Suppose I have a base class with unimplemented methods as follows:
class Polygon():
def __init__(self):
pass
def perimeter(self):
pass
def area(self):
pass
Now, let's say one of my colleagues uses the Polygon class to create a subclass as follows:
import math
class Circle(Polygon):
def __init__(self, radius):
self.radius = radius
def perimeter(self):
return 2 * math.pi * self.radius
(H/Sh)e has forgotten to implement the area() method.
How can I force the subclass to implement the parent's area() method?
python inheritance method-overriding
add a comment |
up vote
19
down vote
favorite
Suppose I have a base class with unimplemented methods as follows:
class Polygon():
def __init__(self):
pass
def perimeter(self):
pass
def area(self):
pass
Now, let's say one of my colleagues uses the Polygon class to create a subclass as follows:
import math
class Circle(Polygon):
def __init__(self, radius):
self.radius = radius
def perimeter(self):
return 2 * math.pi * self.radius
(H/Sh)e has forgotten to implement the area() method.
How can I force the subclass to implement the parent's area() method?
python inheritance method-overriding
add a comment |
up vote
19
down vote
favorite
up vote
19
down vote
favorite
Suppose I have a base class with unimplemented methods as follows:
class Polygon():
def __init__(self):
pass
def perimeter(self):
pass
def area(self):
pass
Now, let's say one of my colleagues uses the Polygon class to create a subclass as follows:
import math
class Circle(Polygon):
def __init__(self, radius):
self.radius = radius
def perimeter(self):
return 2 * math.pi * self.radius
(H/Sh)e has forgotten to implement the area() method.
How can I force the subclass to implement the parent's area() method?
python inheritance method-overriding
Suppose I have a base class with unimplemented methods as follows:
class Polygon():
def __init__(self):
pass
def perimeter(self):
pass
def area(self):
pass
Now, let's say one of my colleagues uses the Polygon class to create a subclass as follows:
import math
class Circle(Polygon):
def __init__(self, radius):
self.radius = radius
def perimeter(self):
return 2 * math.pi * self.radius
(H/Sh)e has forgotten to implement the area() method.
How can I force the subclass to implement the parent's area() method?
python inheritance method-overriding
python inheritance method-overriding
asked Jun 15 '17 at 20:08
Aditya Barve
32728
32728
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
37
down vote
accepted
this could be your parent class:
class Polygon():
def __init__(self):
raise NotImplementedError
def perimeter(self):
raise NotImplementedError
def area(self):
raise NotImplementedError
although the problem will be spotted at runtime only, when one of the instances of the child classes tries to call one of these methods.
a different version is to use abc.abstractmethod.
from abc import ABCMeta, abstractmethod
import math
class Polygon(metaclass=ABCMeta):
@abstractmethod
def __init__(self):
pass
@abstractmethod
def perimeter(self):
pass
@abstractmethod
def area(self):
pass
class Circle(Polygon):
def __init__(self, radius):
self.radius = radius
def perimeter(self):
return 2 * math.pi * self.radius
# def area(self):
# return math.pi * self.radius**2
c = Circle(9.0)
# TypeError: Can't instantiate abstract class Circle with abstract methods area
you will not be able to instantiate a Circle without it having all the methods implemented.
this is the python 3 syntax; in python 2 you'd need to
class Polygon(object):
__metaclass__ = ABCMeta
also note that for the binary special functions __eq__(), __lt__(), __add__(), ... it is better to return NotImplemented instead of raising NotImplementedError.
add a comment |
up vote
3
down vote
That's exactly what NotImplementedError are used for :)
In your base class
def area(self):
raise NotImplementedError("Hey, Don't forget to implement the area!"
add a comment |
up vote
2
down vote
You can raise NotImplementedError exception in base class method.
class Polygon:
def area(self):
raise NotImplementedError
Also you can use @abc.abstractmethod, but then you need to declare metaclass to be abc.ABCMeta, which would make your class abstract. More about abc module
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
37
down vote
accepted
this could be your parent class:
class Polygon():
def __init__(self):
raise NotImplementedError
def perimeter(self):
raise NotImplementedError
def area(self):
raise NotImplementedError
although the problem will be spotted at runtime only, when one of the instances of the child classes tries to call one of these methods.
a different version is to use abc.abstractmethod.
from abc import ABCMeta, abstractmethod
import math
class Polygon(metaclass=ABCMeta):
@abstractmethod
def __init__(self):
pass
@abstractmethod
def perimeter(self):
pass
@abstractmethod
def area(self):
pass
class Circle(Polygon):
def __init__(self, radius):
self.radius = radius
def perimeter(self):
return 2 * math.pi * self.radius
# def area(self):
# return math.pi * self.radius**2
c = Circle(9.0)
# TypeError: Can't instantiate abstract class Circle with abstract methods area
you will not be able to instantiate a Circle without it having all the methods implemented.
this is the python 3 syntax; in python 2 you'd need to
class Polygon(object):
__metaclass__ = ABCMeta
also note that for the binary special functions __eq__(), __lt__(), __add__(), ... it is better to return NotImplemented instead of raising NotImplementedError.
add a comment |
up vote
37
down vote
accepted
this could be your parent class:
class Polygon():
def __init__(self):
raise NotImplementedError
def perimeter(self):
raise NotImplementedError
def area(self):
raise NotImplementedError
although the problem will be spotted at runtime only, when one of the instances of the child classes tries to call one of these methods.
a different version is to use abc.abstractmethod.
from abc import ABCMeta, abstractmethod
import math
class Polygon(metaclass=ABCMeta):
@abstractmethod
def __init__(self):
pass
@abstractmethod
def perimeter(self):
pass
@abstractmethod
def area(self):
pass
class Circle(Polygon):
def __init__(self, radius):
self.radius = radius
def perimeter(self):
return 2 * math.pi * self.radius
# def area(self):
# return math.pi * self.radius**2
c = Circle(9.0)
# TypeError: Can't instantiate abstract class Circle with abstract methods area
you will not be able to instantiate a Circle without it having all the methods implemented.
this is the python 3 syntax; in python 2 you'd need to
class Polygon(object):
__metaclass__ = ABCMeta
also note that for the binary special functions __eq__(), __lt__(), __add__(), ... it is better to return NotImplemented instead of raising NotImplementedError.
add a comment |
up vote
37
down vote
accepted
up vote
37
down vote
accepted
this could be your parent class:
class Polygon():
def __init__(self):
raise NotImplementedError
def perimeter(self):
raise NotImplementedError
def area(self):
raise NotImplementedError
although the problem will be spotted at runtime only, when one of the instances of the child classes tries to call one of these methods.
a different version is to use abc.abstractmethod.
from abc import ABCMeta, abstractmethod
import math
class Polygon(metaclass=ABCMeta):
@abstractmethod
def __init__(self):
pass
@abstractmethod
def perimeter(self):
pass
@abstractmethod
def area(self):
pass
class Circle(Polygon):
def __init__(self, radius):
self.radius = radius
def perimeter(self):
return 2 * math.pi * self.radius
# def area(self):
# return math.pi * self.radius**2
c = Circle(9.0)
# TypeError: Can't instantiate abstract class Circle with abstract methods area
you will not be able to instantiate a Circle without it having all the methods implemented.
this is the python 3 syntax; in python 2 you'd need to
class Polygon(object):
__metaclass__ = ABCMeta
also note that for the binary special functions __eq__(), __lt__(), __add__(), ... it is better to return NotImplemented instead of raising NotImplementedError.
this could be your parent class:
class Polygon():
def __init__(self):
raise NotImplementedError
def perimeter(self):
raise NotImplementedError
def area(self):
raise NotImplementedError
although the problem will be spotted at runtime only, when one of the instances of the child classes tries to call one of these methods.
a different version is to use abc.abstractmethod.
from abc import ABCMeta, abstractmethod
import math
class Polygon(metaclass=ABCMeta):
@abstractmethod
def __init__(self):
pass
@abstractmethod
def perimeter(self):
pass
@abstractmethod
def area(self):
pass
class Circle(Polygon):
def __init__(self, radius):
self.radius = radius
def perimeter(self):
return 2 * math.pi * self.radius
# def area(self):
# return math.pi * self.radius**2
c = Circle(9.0)
# TypeError: Can't instantiate abstract class Circle with abstract methods area
you will not be able to instantiate a Circle without it having all the methods implemented.
this is the python 3 syntax; in python 2 you'd need to
class Polygon(object):
__metaclass__ = ABCMeta
also note that for the binary special functions __eq__(), __lt__(), __add__(), ... it is better to return NotImplemented instead of raising NotImplementedError.
edited Nov 7 at 6:24
answered Jun 15 '17 at 20:12
hiro protagonist
17.3k63560
17.3k63560
add a comment |
add a comment |
up vote
3
down vote
That's exactly what NotImplementedError are used for :)
In your base class
def area(self):
raise NotImplementedError("Hey, Don't forget to implement the area!"
add a comment |
up vote
3
down vote
That's exactly what NotImplementedError are used for :)
In your base class
def area(self):
raise NotImplementedError("Hey, Don't forget to implement the area!"
add a comment |
up vote
3
down vote
up vote
3
down vote
That's exactly what NotImplementedError are used for :)
In your base class
def area(self):
raise NotImplementedError("Hey, Don't forget to implement the area!"
That's exactly what NotImplementedError are used for :)
In your base class
def area(self):
raise NotImplementedError("Hey, Don't forget to implement the area!"
answered Jun 15 '17 at 20:11
Bubble Bubble Bubble Gut
1,899618
1,899618
add a comment |
add a comment |
up vote
2
down vote
You can raise NotImplementedError exception in base class method.
class Polygon:
def area(self):
raise NotImplementedError
Also you can use @abc.abstractmethod, but then you need to declare metaclass to be abc.ABCMeta, which would make your class abstract. More about abc module
add a comment |
up vote
2
down vote
You can raise NotImplementedError exception in base class method.
class Polygon:
def area(self):
raise NotImplementedError
Also you can use @abc.abstractmethod, but then you need to declare metaclass to be abc.ABCMeta, which would make your class abstract. More about abc module
add a comment |
up vote
2
down vote
up vote
2
down vote
You can raise NotImplementedError exception in base class method.
class Polygon:
def area(self):
raise NotImplementedError
Also you can use @abc.abstractmethod, but then you need to declare metaclass to be abc.ABCMeta, which would make your class abstract. More about abc module
You can raise NotImplementedError exception in base class method.
class Polygon:
def area(self):
raise NotImplementedError
Also you can use @abc.abstractmethod, but then you need to declare metaclass to be abc.ABCMeta, which would make your class abstract. More about abc module
edited Jun 15 '17 at 20:18
answered Jun 15 '17 at 20:11
vishes_shell
9,48823345
9,48823345
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44576167%2fforce-child-class-to-override-parents-methods%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password