This question already has answers here:
Is there a builtin identity function in python?
(10 answers)
Closed 7 years ago.
In functional programming is sometimes useful to have an identity function.
Is there a built-in or a function defined in some module that does this?
The identity function can be simply defined as:
identity = lambda x: x
I'm not aware of this function defined in any module, but it could be a good fit for functools.
Related
This question already has answers here:
How to implement built-in sum() of the class?
(2 answers)
Define "sum" for a class using non-associative addition
(2 answers)
Closed 9 months ago.
Is there any way I can over write the sum() method to act differently on a list?
I expected I would be able to find a dunder method that would allow me to define my own implementation, but I couldn't.
That's how I'd imagine the implementation
class MyDummyList(list):
def __sum__(self): -> str:
return 'a'
x = MyDummyList()
print(sum(x)) -> # prints 'a'
Obviously this breaks as there is no dunder method __sum__ but I am somewhat surprised that there is no such a way to do that using dunder methods as sum is a quite common operation on iterables.
This question already has answers here:
What does -> mean in Python function definitions?
(11 answers)
Python3 function definition, arrow and colon [duplicate]
(3 answers)
What does the -> (dash-greater-than arrow symbol) mean in a Python method signature? [duplicate]
(1 answer)
Closed 9 months ago.
I'm analyzing some old code that I've inherited, and I have a question about the use of "self" and "None" keywords, specifically in the following example:
def run(self) -> None:
I understand that the self keyword is similar to the "this" keyword in C++ in that, in conjunction with the dot operator, it allows us to access the attributes and methods of the class in question. What I'm really interested in is the use of "-> None" in the declaration of the method named "run." Is this in PEP 8 because I can't find an example. I'm using Python 3.7, in case that matters.
What is the purpose of writing a method in this manner? What does "-> None" do?
They're called type hints, and they enable annotating the types of the parameters and return types of functions.
https://peps.python.org/pep-0484/
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
I have many functions with the same prefix. I want to use a combination of strings to invoke the function.
def func_plus_one(v):
return v+1
def func_plus_two(v):
return v+2
a='plus_one'
b='plus_two'
So how can I use 'func_'+a and 'func_'+b to use the function?
If the functions are in the same module as the code needing to reference them, use the globals() of the module. You could call the function indicated by a using:
globals()['func_' + a](x)
If they are in another module, use getattr
getattr(some_module, func_' + a)(x)
This question already has answers here:
Is there a builtin identity function in python?
(10 answers)
Closed 3 years ago.
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.
Imho there are two packages that bring identity along many other functional, pure python, and performant methods: funcy and toolz
This question already has answers here:
Why isn't the 'len' function inherited by dictionaries and lists in Python
(7 answers)
Why does Python code use len() function instead of a length method?
(7 answers)
In Python, when should I use a function instead of a method?
(5 answers)
Difference between len() and .__len__()?
(5 answers)
Closed 4 years ago.
In Python, why are some built-in functions called using brackets with the method name before the object, e.g.
print("foobar")
bool("foobar")
...
While others are built-in method attributes, called with a dot behind the object, e.g.
"foobar".capitalize()
Specifically I'm interested to learn if there is a general principle behind this instead of just common practice and memorization. In cases where you can't quite remember whether it was capitalize("foobar") or "foobar".capitalize(), how do you know?