0

I'm trying for the first time to implement __repr__ and __str__ in a class. Then, for the class debugging I tried to print out the class.__repr__() and the class.__str__() value but the printing value is None. Here is the code:

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__()

And here is the output:

None
None

I'm sure I'm doing something wrong. Can someone help me out to figure out the error. And please excuse my English: it is my second language.

Luke__
  • 227
  • 1
  • 9

1 Answers1

0

I think you meant to check the __init__ arguments with this staticmethod:

    @staticmethod
    def _checkargs(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")

        return master, width, height

Then, you call call _checkargs() in the __init__ method and unpack it like so:

self.master, self.width, self.height = self._checkargs(master, width, height)

And your __new__ method does not return anything, so I'd consider removing it.

Also, since your __repr__ and __str__ return the same thing, and when there is no __str__, __repr__ is called therefore you can delete your __str__ method.

Now, in your __repr__ method, you cannot concacanent (add) a Tk or int to a string, so you can change your __repr__ to the following:

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

Lastly, use print(str(object)) instead of print(object.__str__()) and use print(repr(object)) instead of print(object.__repr__())

See also: What is the difference between @staticmethod and @classmethod?

Alan Bagel
  • 818
  • 5
  • 24