[Python] How to extend a class in python?

Discussão em 'Python' iniciado por Stack, Janeiro 31, 2025.

  1. Stack

    Stack Membro Participativo

    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

    Continue reading...

Compartilhe esta Página