how to print class attributes in python 1

how to print class attributes in python

class yourClass:
    def __str__(self):
        return "value"  #replace value with what you want to print

Here is what the above code is Doing:
1. It creates a class called yourClass.
2. It defines a method called __str__.
3. It returns the string “value” from the __str__ method.
4. When you print an instance of yourClass, Python will call the __str__ method and print the string it returns.

Add a __str__ method to the Point class.

The __str__ method should return a string in the following format:

Point(x=1, y=2)

Create an instance of Point called point.

Print point.

Click “Save & Submit Code” and check the output.

Similar Posts