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.
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:
Declaration of list of type python
(1 answer)
Using List/Tuple/etc. from typing vs directly referring type as list/tuple/etc
(2 answers)
Closed 10 months ago.
After several hours of chasing this error with an AWS Elastic Beanstalk app (Python 3.8), I learned that I can't use type hints with brackets, as seen below. But I don't understand why.
Per the docs, I see type hints as far back as at least 3.5 (I didn't look back further than this).
Does this have to do with the Python version, or something else?
doesn't work:
def func(x, y:list[str]):
pass
works:
def func(x, y:list):
pass
This question already has answers here:
What are type hints in Python 3.5?
(5 answers)
What does -> mean in Python function definitions?
(11 answers)
What are variable annotations?
(2 answers)
Closed 2 years ago.
I am new to Python and I have the next questions in the below code line. Please help.
def method(self, a : dict) -> list:
1st question: what does it mean to declare a: dict?
2nd question: What does that arrow (-> list) means/indicates/represents?
To be honest I just have an idea about the first thing (a: dict), but I need the complete picture about how that way of writing that kind of python method works.
Thanks in advance.
So these are type hints.
a : dict is saying that a should be of type dict
and -> list is saying the function will return the type list
The documentation for this can be found here
a : dict means the argument 'a' for the function is of dictionary type.
`-> list' means that this function will return a list variable.
This question already has answers here:
Is there a way to set a default parameter equal to another parameter value?
(4 answers)
Closed 4 years ago.
I have the following function declaration:
def set_data(obj=None, name='delp', type=None):
Is there a python shortcut, where type is equal with 'name' value(default r passed) if is None ?
The usual idiom is:
def set_data(obj=None, name='delp', type=None):
if type is None:
type = name
(But don't actually use type as a variable name, it'll mask the built in function as described here: Is it safe to use the python word "type" in my code?)
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?