Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Methods can be attached to an enum type. Thus, instead of defining an independent function that takes an enum as argument (i.e.: my_function(my_enum_value)), we can define a method attached to the enum type. We will call this method directly onto the object: my_enum_value.my_method().

Let us define a Suit enum for representing card suits:

Using the impl statement we declare the is_black() method that returns true for black color cards (i.e.: clubs and spades):

We can call this method on any Suit instance:

Club
Spade
()

see solution