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
  • 15,737
  • 15
  • 35
  • 52
geckos
  • 4,912
  • 1
  • 34
  • 40

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,104
  • 1
  • 19
  • 24