Python Object Oriented Programming | Static Methods

Static methods are similar to class methods, except they don’t receive any additional arguments; they are identical to normal functions that belong to a class. They are marked with the static-method decorator.


class Pizza:
	def __init__(self, toppings):
		self.toppings = toppings

	def validate_topping(topping):
		if topping == "pineapple":
			raise ValueError("No pineapples!")
			return True

ingredients = ["cheese", "onions", "spam"]
if all(Pizza.validate_topping(i) for i in ingredients):
	pizza = Pizza(ingredients)

Static methods behave like plain functions, except for the fact that you can call them from an instance of the class.

Leave a Reply