In python how can you extend a class? For example if I have
color.py
class Color:
def __init__(self, color):
self.color = color
def getcolor(self):
return self.color
color_extended.py
import Color
class Color:
def getcolor(self):
return self.color + " extended!"
But this doesn't work...
I expect that if I work in color_extended.py
, then when I make a color object and use the getcolor
function then it will return the object with the string " extended!" in the end. Also it should have gotton the init from the import.
Assume python 3.1
Thanks
Use:
import color
class Color(color.Color):
...
If this were Python 2.x, you would also want to derive color.Color
from object
, to make it a new-style class:
class Color(object):
...
This is not necessary in Python 3.x.