@@ -600,47 +600,39 @@ Note:
600
600
* ` all() ` returns ` True ` when * all* boolean values/expressions in the sequence are ` True `
601
601
* ` any() ` returns ` True ` when * any* boolean values/expressions in the sequence are ` True `
602
602
603
- ## More Functions
604
603
605
- ``` {index} single: Python; Functions
606
- ```
604
+ ## Coding Style and Documentation
607
605
608
- Let's talk a bit more about functions, which are all important for good programming style.
606
+ A consistent coding style and the use of
607
+ documentation can make the code easier to understand and maintain.
609
608
610
- ### The Flexibility of Python Functions
609
+ ### Python Style Guidelines: PEP8
611
610
612
- As we discussed in the {ref}` previous lecture <python_by_example> ` , Python functions are very flexible.
611
+ ``` {index} single: Python; PEP8
612
+ ```
613
613
614
- In particular
614
+ You can find Python programming philosophy by typing ` import this ` at the prompt.
615
615
616
- * Any number of functions can be defined in a given file.
617
- * Functions can be (and often are) defined inside other functions.
618
- * Any object can be passed to a function as an argument, including other functions.
619
- * A function can return any kind of object, including functions.
616
+ Among other things, Python strongly favors consistency in programming style.
620
617
621
- We already {ref}` gave an example <test_program_6> ` of how straightforward it is to pass a function to
622
- a function.
618
+ We've all heard the saying about consistency and little minds.
623
619
624
- Note that a function can have arbitrarily many ` return ` statements (including zero).
620
+ In programming, as in mathematics, the opposite is true
625
621
626
- Execution of the function terminates when the first return is hit, allowing
627
- code like the following example
622
+ * A mathematical paper where the symbols $\cup$ and $\cap$ were
623
+ reversed would be very hard to read, even if the author told you so on the
624
+ first page.
628
625
629
- ``` {code-cell} python3
630
- def f(x):
631
- if x < 0:
632
- return 'negative'
633
- return 'nonnegative'
634
- ```
626
+ In Python, the standard style is set out in [ PEP8] ( https://www.python.org/dev/peps/pep-0008/ ) .
635
627
636
- Functions without a return statement automatically return the special Python object ` None ` .
628
+ (Occasionally we'll deviate from PEP8 in these lectures to better match mathematical notation)
637
629
638
630
### Docstrings
639
631
640
632
``` {index} single: Python; Docstrings
641
633
```
642
634
643
- Python has a system for adding comments to functions, modules , etc. called * docstrings* .
635
+ Python has a system for adding comments to modules, classes, functions , etc. called * docstrings* .
644
636
645
637
The nice thing about docstrings is that they are available at run-time.
646
638
@@ -691,71 +683,7 @@ def f(x):
691
683
692
684
With one question mark we bring up the docstring, and with two we get the source code as well.
693
685
694
- ### Keyword Arguments
695
-
696
- ``` {index} single: Python; keyword arguments
697
- ```
698
-
699
- In a {ref}` previous lecture <python_by_example> ` , you came across the statement
700
-
701
- ``` {code-block} python3
702
- :class: no-execute
703
-
704
- plt.plot(x, 'b-', label="white noise")
705
- ```
706
-
707
- In this call to Matplotlib's ` plot ` function, notice that the last argument is passed in ` name=argument ` syntax.
708
-
709
- This is called a * keyword argument* , with ` label ` being the keyword.
710
-
711
- Non-keyword arguments are called * positional arguments* , since their meaning
712
- is determined by order
713
-
714
- * ` plot(x, 'b-', label="white noise") ` is different from ` plot('b-', x, label="white noise") `
715
-
716
- Keyword arguments are particularly useful when a function has a lot of arguments, in which case it's hard to remember the right order.
717
-
718
- You can adopt keyword arguments in user-defined functions with no difficulty.
719
-
720
- The next example illustrates the syntax
721
-
722
- ``` {code-cell} python3
723
- def f(x, a=1, b=1):
724
- return a + b * x
725
- ```
726
-
727
- The keyword argument values we supplied in the definition of ` f ` become the default values
728
-
729
- ``` {code-cell} python3
730
- f(2)
731
- ```
732
-
733
- They can be modified as follows
734
-
735
- ``` {code-cell} python3
736
- f(2, a=4, b=5)
737
- ```
738
-
739
- ## Coding Style and PEP8
740
-
741
- ``` {index} single: Python; PEP8
742
- ```
743
-
744
- To learn more about the Python programming philosophy type ` import this ` at the prompt.
745
-
746
- Among other things, Python strongly favors consistency in programming style.
747
-
748
- We've all heard the saying about consistency and little minds.
749
-
750
- In programming, as in mathematics, the opposite is true
751
-
752
- * A mathematical paper where the symbols $\cup$ and $\cap$ were
753
- reversed would be very hard to read, even if the author told you so on the
754
- first page.
755
-
756
- In Python, the standard style is set out in [ PEP8] ( https://www.python.org/dev/peps/pep-0008/ ) .
757
-
758
- (Occasionally we'll deviate from PEP8 in these lectures to better match mathematical notation)
686
+ You can find conventions for docstrings in [ PEP257] ( https://peps.python.org/pep-0257/ ) .
759
687
760
688
## Exercises
761
689
0 commit comments