Почему __repr__ и __str__ возвращают значение None? - PullRequest
0 голосов
/ 09 октября 2019

Я впервые пытаюсь реализовать __repr__ и __str__ в классе. Затем для отладки класса я попытался распечатать значения class.__repr__() и class.__str__(), но значение печати равно None. Вот код:

class Window(object):
    ''' implements some methods for manage the window '''


    def __new__(cls, master, width=1000, height=500):
        ''' check if the variables to pass to the __init__ are the correct data type '''

        # checking if the passed arguments are the correct type
        if not isinstance(master, Tk):
            raise TypeError("master must be Tk class type")

        if not isinstance(width, int):

            if isinstance(width, float):
                width = int(width)

            else:
                raise TypeError("width must be integer")

        if not isinstance(height, int):

            if isinstance(height, float):
                height = int(height)

            else:
                raise TypeError("width must be integer")


    def __init__(self, master, width=1000, height=500):
        ''' initialize the wnidow and set his basic options '''

        self.master = master
        self.width = width
        self.height = height


    def __repr__(self):
        repr_to_return = "__main__.Window{master=" + self.master + ", width=" + self.width + ", height=" + self.height + "}"
        return repr_to_return


    def __str__(self):
        str_to_return = "__main__.Window(master=" + self.master + ", width=" + self.width + ", height=" + self.height + ")"
        return str_to_return


# checking if the script has been executed as program or as module
if __name__ == "__main__":

    # declaring a Tk object
    root = Tk()
    win = Window(root, width=1000, height=500)

    win.__str__()

А вот и вывод:

None
None

Я уверен, что делаю что-то не так. Может кто-нибудь помочь мне разобраться в ошибке. И прошу прощения за мой английский: это мой второй язык.

...