Skip to content

mightbesimon/lifehacks.metaclasses

Repository files navigation

lifehacks.metaclasses version

sponsor downloads publish

Structure

📦 lifehacks.metaclasses
├── meta
├── enum
└── EnumException

Installation

pip install lifehacks.metaclasses

Usage

meta metaclass

There are a few ways to make an meta class.

from lifehacks.metaclasses import meta

class myenum0(metaclass=meta): ...

@meta  		# preferred method
class myenum1: ...

@meta()		# do not use this method
class myenum2: ...

enum metaclass

Same with meta metaclass from above, there are a few ways to make an enum class.

from lifehacks.metaclasses import enum

# with typing
class MyPalette0(metaclass=enum[Colour]): ...

@enum  		# preferred method
class MyPalette1: ...

@enum[Colour]	# syntax only allowed python>=3.9
class MyPalette2: ...

@enum()		# do not use this method
class MyPalette3: ...

usage example:

from lifehacks.metaclasses import enum

@enum[Colour]
class BasePalette:
	BLACK = ...
	WHITE = ...

# extending base palette enum
class SubPalette(BasePalette):
	RED = ...
	GREEN = ...

# can be used in type hinting
def print_colours(palette:enum[Colour]) -> None:
	for name, value in palette:
		print(name, value)

print_colours(BasePalette) # BLACK, WHITE
print_colours(SubPalette)  # BLACK, WHITE, RED, GREEN
print(BasePalette.BLACK in SubPalette) # True

enum classes are not instantiable, more on that below.

EnumException

enum classes are not instantiable. If you try to, you get an EnumException

@enum
class Palette: ...

p = Palette()	# illegal, raises EnumException

Contributors