![]() ![]() We’ll check off each completed step as we work through this material.įor Step 1, Create a distribution description, we need to create twoįiles that we’ll place in the same folder as our vsearch.py file. Even the most beautifully written docstring will still have to be read if it doesn’t include annotations. This is what they’ll have to do if annotations aren’t used. ![]() CONTACT BOOK IN PYTHON SOURCE CODE CODEWhat annotations do for programmers using your function is rid them of the need to read your function’s code to learn what types are expected by, and returned from, your function. The type of the data being passed back and forth is not considered by the interpreter. The interpreter calls your function with whatever arguments are provided to it (no matter their type), executes your function’s code, and then returns to the caller whatever value it is given by the return statement. In fact, the interpreter does not care what type your arguments are, nor does it care what type of data your function returns. Annotations are a documentation standard, not a type enforcement mechanism. The goal of annotations is not to make life easier for the interpreter it’s to make life easier for the user of your function. If the Python interpreter isn’t going to use your annotations to check the types of your function’s arguments and its return type, why bother with annotations at all? Starting with a simple example ensures we have the basics in place first, before moving on. This is a bit of a departure from our function’s existing functionality, but bear with us, as we are going to build up to something more complex (and useful) in a bit. Specifically, let’s return either True or False depending on whether the word supplied as an argument contains any vowels. ![]() Let’s start with a straightforward example of returning a single value from our search4vowels function. ![]() This behavior mimics how return works in the majority of other programming languages. When the interpreter encounters a return statement in your function’s suite, two things happen: the function terminates at the return statement, and any value provided to the return statement is passed back to your calling code. To support returning a value (or values) from a function, Python provides the return statement. That said, and above all else, your usage should be consistent.Īs well as using a function to abstract some code and give it a name, programmers typically want functions to return some calculated value, which the code that called the function can then work with. Which character you use to enclose your strings is up to you, although using the single quote character is very popular with the majority of Python programmers. Strings enclosed by a single quote character ( ') or a double quote character ( ") cannot span multiple lines: you must terminate the string with a matching quote character on the same line (as Python uses the end of the line as a statement terminator). Docstrings have an interesting characteristic in that they can span multiple lines (other programming languages use the name “heredoc” for the same concept). Even though you can use """ or ''' to surround your docstrings, most Python programmers prefer to use """. In Python, strings can be enclosed in a single quote character ( '), a double quote character ( "), or what’s known as triple quotes ( """ or ''').Īs mentioned earlier, triple quotes around strings are known as docstrings, because they are mainly used to document a function’s purpose (as shown above). Understanding the string quote characters ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |