SoFunction
Updated on 2024-11-15

The reason why the output of a class or instance of a class in python is of this form

Reason:The result of the special method __str__() that converts an object to a string

Rendering:

Code:

# Define a Person class
class Person(object):
  """Human."""
  def __init__(self, name , age):
     = name
     = age

p = Person(‘Blackie‘,18)
print(p)

print(‘\n\n\n\n\n‘)

# Define a Person class
class Person(object):
  """Human."""
  def __init__(self, name , age):
     = name
     = age

  # __str__() This special method is called when trying to convert an object to a string
  # It can be used to specify the result of converting an object to a string (print function)
  def __str__(self):
    print(Person)
    return ‘Person [name=%s , age=%d]‘%(,) 

p = Person(‘Blackie‘,18)
print(p)

Why is the output of a class or instance of a class in python of the form <__main__class name object at xxxx>?