Double Under (Dunder) Methods

The string Dunder method:

class Employee:
	def __init__(self, name, age):
		self.name = name
		self.age = age
		
	def __str__(self):
		return self.name + " Age is " + str(self.age)
		
tom = Employee("Tom Lanister", 47)
print(tom)
  • Returns the variables as strings

  • Printing: "Tom Lanister Age is 47"

The len Dunder method:

	def __len__(self):
		return self.age
  • Returns the len of the variable specified

Good collection of other Dunder methods

Last updated