pydentity
is an implementation of identity functions designed
for various practical applications in Python development
and functional programming.
The identity function is a generic function that takes a value of any type and returns it unchanged.
from src.identity import identity
identity('hello') # returns "hello"
identity(1) # returns 1
This function takes a type as an argument and returns a type-specific identity function. If the type-specific
function is called with a value that doesn't match the specified type, it will raise a TypeError
.
from src.identity import create_identity
string_identity = create_identity(str)
string_identity('hello') # returns "hello"
string_identity(1) # raises TypeError: Expected value of type "str", got "int"
A wide range of built-in types are supported:
bool
bytes
dict
ellipsis
float
int
list
set
str
tuple
NoneType
The identity
function can be used as a no-operation placeholder during
the development process, especially in the early stages when components are pending implementation.
The identity
function can be employed as a default function, a fallback option for when a function is not supplied
during development.
The create_identity
function ensures type safety by generating
type-specific identity functions in scenarios where type consistency is required.
Identity functions can be used as arguments for higher-order functions (HOFs), which accept other functions as parameters.
Identity functions, which leave their input unaltered, can be utilized in functional composition (data transformation pipelines), where a certain transformation might conditionally need to be skipped.