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/
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 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:
What does -> mean in Python function definitions?
(11 answers)
Closed 3 years ago.
What does -> do in Python? Can't seem to find anything with a search. Most I can guess is that it creates an alias of some kind.
def function(parameters) -> str
or
format(...)
S.format(*args, **kwargs) -> str
In python 3.5+ optioinal typing was included. Mypy is the library used for extra typing support. After a function declaration "->" is used to specify the type of what the function returns.
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?
This question already has answers here:
Get a function argument's default value?
(7 answers)
How to find out the default values of a particular function's argument in another function in Python?
(4 answers)
How can I read a function's signature including default argument values?
(9 answers)
Closed 5 years ago.
I am a python beginner and I currently struggle with some (probably quite simple) stuff. I would like to know the default parameters of a python function, more specifically of cv2.ORB_create().
With the following code I managed to see the 'documentation string':
import cv2
orb = cv2.ORB_create()
print(cv2.ORB_create.__doc__)
However, print(cv2.ORB_create.__defaults__) just gives me an error;
AttributeError: 'builtin_function_or_method' object has no attribute '__defaults__'
Maybe I am missing a link between functions, modules, etc. but I am really stuck...
Since the concern was brought up that this is a duplicate. I also tried inspect.signature and by extension inspect.getargspec, but this give me another error ValueError: no signature found for builtin <built-in function ORB_create>.
cv2.ORB_create() seems to be a function written with the Python extensions (In C instead of Python). As such, it isn't a "normal" function object, and there is no way to see the default values reliably (As it is handled manually in C).
One possible solution would be to look at the __text_signature__ attribute, but this may not be reliable. See What are __signature__ and __text_signature__ used for in Python 3.4.