This question already has answers here:
What does the Ellipsis object do?
(14 answers)
Closed 3 years ago.
Well...When I define a function that does not perform certain actions I don't understand what the difference between ... and pass. For example, the following two pieces of code:
def _clear() -> None: ...
def _clear() -> None: pass
They are the same, right? Either one can be used? I sincerely hope someone can help me thoroughly understand it. Thanks...
pass is the standard way to say that a function does not do anything.
Placing the ellipsis object does yield valid code just like def _clear() -> None: x = 5, but most IDEs and static analysis tools will complain that the statement has no effect.
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 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:
What are variable annotations?
(2 answers)
Closed 3 years ago.
I just came across this function:
def splitComma(line: str):
splits = Utils.COMMA_DELIMITER.split(line)
return "{}, {}".format(splits[1], splits[2])
I am aware that you can separate parameters by , or can set a value within a parameter like a=39 but I have not seen a colon like line:str. I have checked the function definition online but could not find anything like this. What does this colon mean?
It's a function annotation; function arguments and the return value can be tagged with arbitrary Python expressions. Python itself ignores the annotation (other than saving it), but third-party tools can make use of them.
In this case, it is intended as type hint: programs like mypy can analyze your code statically (that is, without running it, but only looking at the source code itself) to ensure that only str values are passed as arguments to splitComma.
A fuller annotation to also specify the return type of the function:
def splitComma(line: str) -> str:
...
(Note that originally, function annotations weren't assumed to have any specific semantics. This is still true, but the overwhelming assumption these days is that the annotations provide type hints.)
This is a type annotation used by static analysis tools to check, well, types. It helps ensure program correctness before you run the code.
This question already has answers here:
What does it mean to "call" a function in Python? [closed]
(4 answers)
Closed 5 years ago.
I am new to programming, and trying out with a little Python3.
I have some trouble understanding the concept behind calling a function? having the defined the following function, what would be the proper way to call it?
def string_length(mystring):
return len(mystring)
Thanks in advance guys
def string_length(mystring):
return len(mystring)
print(string_length('something'))
Like that
length = string_length('some_string')
where length is a variable that will store the output.
This question already has answers here:
Using pass on a non necessary else statement
(5 answers)
Closed 8 years ago.
Is there a preferred/proper style?
This:
def fx(Boolean):
if Boolean:
# Do stuff.
else:
pass
Or this:
def fx(Boolean):
if Boolean:
# Do stuff.
Is it preferred/proper to include else: pass if you don't want anything to happen?
I've read PEP 8 - Style Guide for Python Code and did not find anything concerning my question.
You should never include else: pass. It's superfluous. Just omit the else; it's intentionally an optional keyword.
If you don't need the else if there is no reason to add it. It will just confuse other people reading your code in the future (ie yourself a few months later).