1
2class Person:
3 name = ""
4 age = 0
5
6 def __init__(self, personName, personAge):
7 self.name = personName
8 self.age = personAge
9
10 def __repr__(self):
11 return {'name':self.name, 'age':self.age}
12
13 def __str__(self):
14 return 'Person(name='+self.name+', age='+str(self.age)+ ')'
15
1>>>x=4
2>>>repr(x)
3'4'
4>>>str(x)
5'4'
6>>>y='stringy'
7>>>repr(y)
8"'stringy'"
9>>>str(y)
10'stringy'
1# The repr() function returns a printable representational string of the given object.
2>>> var = 'foo'
3>>> print(repr(var))
4"'foo'"
1import datetime
2now = datetime.datetime.now()
3now.__str__()
4#>>> '2020-12-27 22:28:00.324317'
5now.__repr__()
6#>>> 'datetime.datetime(2020, 12, 27, 22, 28, 0, 324317)'
1#!/bin/python3
2
3import math
4import os
5import random
6import re
7import sys
8
9class Car:
10 def _init_(self,speed,unit):
11 self.speed=speed
12 self.unit=unit
13 def __str__(self):
14 return "Car with the maximum speed of {} {}".format(self.speed,self.unit)
15
16class Boat:
17 def _init_(self,speed):
18 self.speed=speed
19 def __str__(self):
20 return "Boat with the maximum speed of {} knots".format(self.speed)
21
22
23check the indentation of Boat class __str__() method, it should be as shown below.
24
25class Boat:
26 def _init_(self,speed):
27 self.speed=speed
28 def __str__(self):
29 return "Boat with the maximum speed of {} knots".format(self.speed)