0

Is there any function in python standard library that returns its argument, something similar to lambda x: x and a method that returns its self?

My motivation is to implement something like:

def identity(x):
    return x

def default(arg, default, func=identity):
    return func(arg) if arg is not None else func(default)

Ruby has itself that would be something like:

class Self:
    def itself(self):
        return self

There are other use cases for a function/method composition that returns its arguments or its self.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
geckos
  • 5,687
  • 1
  • 41
  • 53

1 Answers1

1

Imho there are two packages that bring identity along many other functional, pure python, and performant methods: funcy and toolz

Drey
  • 3,314
  • 2
  • 21
  • 26