This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Understanding Python decorators
What function does the "class decorator"/"method decorator" (#) serve? In other words, what is the difference between this and a normal comment?
Also, what does setter do when using #previousMethod.setter before a method? Thank you.
#decorator
def function(args):
#body
is just syntactic sugar for:
def function(args):
#body
function = decorator(function)
That's really it.
As you see, the decorator gets called, so it's by no means a comment.
Related
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:
What does the "at" (#) symbol do in Python?
(14 answers)
Closed 2 years ago.
I was seeing the methods and docs of the built in super() method of python using the help() function in the IDLE .
I came across this piece of code
This works for class methods too: | class C(B): | #classmethod | def cmeth(cls, arg): | super().cmeth(arg)
In the second line , you can see the # sign before classmethod .
What does the # symbol does in python and what are its uses ?
The # character denotes a decorator. Decorators are functions that can modify or extend behavior of another function temporarily by wrapping around them.
Decorators wrap around a function by receiving them as a parameter. The # syntax (also known as "pie" syntax) applies the classmethod decorator to cmeth after it is defined in your snippet.
You can read more about the specific decorator from your example (classmethod) here.
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 an answer here:
Is it possible to override the assignment ('=') operator in Python?
(1 answer)
Closed 5 years ago.
I found out yesterday that it is possible to override operators in python, so after a bit of googling i found out how, but i could not find any way of overloading the "=" sign. There is __set__() but as i understand it, it overloads the sign for attributes in the object, and not for the object itself.
What i want to accomplish is this:
F = Foo(1)
G = Foo(2)
F = G #overloaded =
So is there a way of overloading "=" for an object in python? (and what is that function called)
You cannot overload =(assign) operator.
This question already has answers here:
Get function callers' information in python
(3 answers)
How to get the caller's method name in the called method?
(11 answers)
Closed 5 years ago.
I've been thinking about this question for a while, and I can't seem to find any related questions to it, probably because I don't know the proper terminology for what I'm looking for.
Is there any condition for an if-else statement in a function that is reliant on which other function is doing the calling? Such as:
def FUNC():
if func1 called FUNC:
statement
elif func2 called FUNC:
statement
def func1():
FUNC()
def func2():
FUNC()
I'm not sure what purpose there would be behind doing this, but I just wanted to know if it was an available option. Thank you.
Solution: pass an argument.
def FUNC(who_called_me):
if who_called_me == 'func1':
# statement
elif who_called_me == 'func2':
# statement
def func1():
FUNC(who_called_me='func1')
def func2():
FUNC(who_called_me='func2')
The argument (who_called_me) does not have to be of type string, but can also be an integer, boolean or an object.