Answers for "<__main__.boat object at 0x7fc678e66cd0> even after using __str__"

8

override python print for class

>>> class Test:
...     def __repr__(self):
...         return "Test()"
...     def __str__(self):
...         return "member of Test"
... 
>>> t = Test()
>>> t
Test()
>>> print(t)
member of Test
Posted by: Guest on August-15-2020
3

python __repr__

class Person:
    name = ""
    age = 0

    def __init__(self, personName, personAge):
        self.name = personName
        self.age = personAge

    def __repr__(self):
        return {'name':self.name, 'age':self.age}

    def __str__(self):
        return 'Person(name='+self.name+', age='+str(self.age)+ ')'
Posted by: Guest on March-08-2020
0

<__main__.boat object at 0x7fc678e66cd0> even after using __str__

#!/bin/python3

import math
import os
import random
import re
import sys

class Car:
    def _init_(self,speed,unit):
        self.speed=speed
        self.unit=unit
    def __str__(self):
        return "Car with the maximum speed of {} {}".format(self.speed,self.unit)  

class Boat:
    def _init_(self,speed):
        self.speed=speed
    	def __str__(self):
        	return "Boat with the maximum speed of {} knots".format(self.speed)  
            
            
check the indentation of Boat class __str__() method, it should be as shown below.
  
class Boat:
    def _init_(self,speed):
        self.speed=speed
    def __str__(self):
        return "Boat with the maximum speed of {} knots".format(self.speed)
Posted by: Guest on November-19-2020

Code answers related to "<__main__.boat object at 0x7fc678e66cd0> even after using __str__"

Python Answers by Framework

Browse Popular Code Answers by Language