View the Index
Total Page:16
File Type:pdf, Size:1020Kb
INDEX Numbers and Symbols __(double underscore), using in 256 objects and 257 objects, 154–155 dunder methods, 322. See also ./, using with Ubuntu, 42 underscore (_) /? command line argument, 25–26 / (forward slash) = assignment operator, 113 purpose of, 18 == comparison operator, 113, 336 using with command line chaining, 103, 105 arguments, 25 using to compare objects, 154 # (hash mark) using with None, 94–95 using with comments, 183 != comparison operator, 336 using with docstrings, 188 * and ** syntax [] (index operator), using, 117 using to create variadic functions, ; (semicolons), using with timeit 167–171 module, 226–227 using to create wrapper functions, ' (single quote), using, 46 171–172 ~ (tilde), using in macOS, 23 using with arguments and - (unary operator), 155–157 functions, 166–167 + (unary operator), 156–157 * character, using as wildcard, 28–29 _ (underscore) ? character, using as wildcard, 28–29 PEP 8’s naming conventions, [:] syntax, using, 104 60–61 < comparison operator, 337 as prefix for methods and <= comparison operator, 337 attributes, 291–292 > comparison operator, 337 private prefix, 81 >= comparison operator, 337 using with _spaces attribute, 290 -> (arrow), using with type hints, 191 using with dunder methods, 120 \ (backslash) using with private attributes and purpose of, 18 methods, 283 using with strings, 95 using with WizCoin class, 279 : (colon), using with lists, 97–98, 104 , (comma), including in single-item A tuples, 150 abcTraceback.py program, saving, 4 - (dash), using with command line __abs__() numeric dunder arguments, 25 method, 328 $ (dollar sign), using in macOS, 23 absolute versus relative paths, 20–21 . (dot), using with commands, 31 __add__() numeric dunder method, 327 -- (double dash), using with command addToTotal() function, creating, line arguments, 25 172–173 algebra for big O, 236 base class, relationship to algorithm analysis. See big O algorithm inheritance, 296 aliases, defined, 121 Bash file, 22–23 all() function, 157–158 BFG Repo-Cleaner tool, 220 alphabetical sort, performing, 146–147 big O algorithm ALT key. See keyboard shortcuts algebra, 236 Amdahl’s Law, 229–230 algorithm analysis, 230 and operator, using, 103 analysis, 230 __and__() numeric dunder method, 328 analysis examples, 239–242 anonymous functions, 174 –175 analyzing, 243–244 answer archive, building with Stack best practice, 244 Overflow, 12 bookshelf metaphor for orders, antigravity feature, enabling, 160 231–235 API (application programming determining order for code, interface), 130 237–244 API vs. library vs. framework vs. SDK vs. doing analysis, 236 engine, 130 function calls, 242–243 append() method, using, 115 lower and higher orders, 230–231 applications, opening, 22 lower orders and coefficients, arguments. See also positional 238–239 arguments math for analysis, 236 vs. parameters, 128 measuring worst-case passing to functions, 166–167 scenario, 235 setting defaults for parameters, “n” is small, 244 142–143 order of function calls, 242–243 arrow (->), using with type hints, 191 orders of notation, 245 ASCII (American Standard Code for big Omega notation, 235 Information Interchange), 146 big Theta notation, 235 ASCII art, using in Four-in-a-Row binary code, explained, 129 game, 272 Black code formatting tool AsciiArt class, creating, 304–305 adjusting line length setting, 55 assignment and comparison operators, described, 45 chaining, 103, 105 disabling double-quoted strings atoms and literals, 110 setting, 55–56 attributes disabling for parts of code, 57 as backing fields, 317–318 installing, 54 defined, 278, 282–284 previewing changes, 56–57 vs. properties, 128–129 running from command line, turning into properties, 316–319 54–57 vs. variable, 124 semantic decisions, 58 syntactic decisions, 58 B block vs. clause vs. body, 123–124 backing field, relationship to attributes, __bool__() numeric dunder method, 328 317–318 Boolean values, 158–159. See also backporting type hints, 196 values backslash (\) Bourne Shells, 22–23 purpose of, 18 bugs, types of, 109, 150–151 using with strings, 95 bytecode vs. machine code, 129 340 Index C code formatting, defined, 45 C:\ part of path, 18 code point, getting for characters, -c switch, using to run code from 146 –147 command line, 26 code smells. See also source code callables and first-class objects, 121–122 classes as functions or modules, 77 camelCase, 60 commented-out code, 74–75 casing styles, 60 dead code, 74–75 Catalina version, 23 defined, 69 cd (change directories) command, duplicate code, 70–71 29–30 empty except blocks, 79–80 __ceil__() numeric dunder error messages, 79–80 method, 328 list comprehensions, 77–79 chaining operators, 103, 105, 159–160 magic numbers, 71–73 child class, creating, 294 myths, 80–84 class attributes, 306 print debugging, 75–76 class methods, 304–306 summary, 84–85 class objects, 284 variables with numeric suffixes, 76 classes. See also inheritance codetags and TODO comments, 187 creating, 77 coercion, explained, 128 creating objects from, 278 collections module, contents of, 120 creating WizCoin, 279–284 collections.defaultdict, using for defined, 276 default values, 99–100 designing for real world, 290–291 colon (:), using with lists, 97–98, 104 as functions or modules, 77 comma (,), including in single-item “is a” relationships, 299 tuples, 150 clause vs. block vs. body, 123–124 command history, viewing, 28 CLI (command line interface), 22 command line close() and open() functions, 93–94 arguments, 24–26 cls and clear (clear terminal) options, 25 commands, 35 overview, 22–23, 42 code. See also source code running code with -c switch, 26 avoiding guesses, 90 running programs from, 23–24 beauty of, 88 running py.exe program, 26–27 commented out, 74–75 running Python programs flat vs. nested, 89 from, 26 formatting for readability, 12–13 tab completion, 27–28 implementation, 90 terminal window, 23 interrupting, 134 Command Prompt shell, 23 namespaces, 91 commands organizing, 77 canceling, 28 readability of, 89 cd (change directories), 29–30 running from command line, 26 cls and clear (clear terminal), 35 silenced errors, 89–90 copy and cp (copy files), 31–32 simplicity and complexity of, 89 del (delete files and folders), 33–34 sparse vs. dense, 89 dir (list folder contents), 30 special cases, 89 dir /s (list subfolder contents), 31 speed of, 90 find (list subfolder contents), 31 verbose and explicit, 89 ls (list folder contents), 30 Index 341 commands (continued) CPython implementation, 108 md and mkdir (make folders), 34 CTRL key. See keyboard shortcuts move and mv (move files), 32 cwd (current working directory), mv (rename files and folders), 19–20, 31 32–33 rd and rmdir (delete folders), D 34–35 dash (-) using with command line ren (rename files and folders), arguments, 25 32–33 data, validating with setters, 319 rm (delete files and folders), 33–34 data types running from Python defined, 276 programs, 27 and return values, 177–178 shortened names, 32 dead code, 74–75 where (find programs), 35 decimal.Decimal(), passing integers to, which (find programs), 35 148–149 wildcard characters, 28–29 decrement and increment operators, commented-out code, 74–75 156–157 comments default arguments, setting for best practices, 197 parameters, 142–143, 165–166 myth about, 83–84 default values, using collections using, 182–188 .defaultdict for, 99–100 using with type hints, 196 deleting commit history, rewriting in Git, 220 files and folders, 33–34 commit log, viewing in Git, 216–217 files from repo, 214–215 commits, rolling back in Git, 218–220 folders, 34–35 comparing objects, 154–155 items from list, 134–140 comparison operators. See also and moving files in repo, 215–216 sequence comparisons derived class, relationship to chaining with assignment inheritance, 296 operators, 103, 105 deterministic function, 173. See also function form of, 333 functions comparisons, making, 94–95 dictionaries __complex__() numeric dunder get() and setdefault() method, 328 methods, 104 composition vs. inheritance, 299–301 key-value pairs, 118 conditional expressions, ternary Python mailing list discussion, 131 operator, 101–102 setting type hints for, 195–196 containers, defined, 119–120 using, 98–101 Cookiecutter, using to create projects, using as default arguments, 200–202 143–144 copy and cp commands, 31 diff program, seeing changes with, copy.copy() and copy.deepcopy(), using 211–212 with mutable values, 140–142 dir command, using, 30 copying dir /s command, 31 files and folders, 31 displays and literals, 110 mutable values, 140–142 __divmod__() numeric dunder cProfile profiler, 154, 228–230 method, 327 CPU, instruction set of, 129 docs folder, contents of, 200 342 Index docstrings errors, preventing with linters, 8–9 defined, 182 exceptions summary, 197 catching, 79–80 using, 188–190 raising, 4, 90, 178–179 dollar sign ($), using in macOS, 23 RecursionError, 318–319 doskey program, 28 explanatory comments, 184–185 dot (.), using with commands, 31 expressions vs. statements, 122–123 double dash (--), using with command line arguments, 25 F double-free bugs, 109 False and True keywords, 158–159 double underscore (__), using in FAQs (Frequently Asked Questions), 10 dunder methods, 322. See also filenames, as command line underscore (_) arguments, 25 dunder methods. See also methods; OOP filenames and folders, matching with (object-oriented programming) wildcards, 28–29 comparison, 332–337 file paths, specifying, 20–21 defined, 120 files numeric, 325–328 copying, 31 in-place augmented assignment, deleting, 33–34 330–332 moving, 32 reflected numeric, 328–330 renaming, 32–33 string representation, 323–325 filesystem, 18–21, 42 using, 322–323 filtering with list comprehensions, duplicate code, 70–71 175 –176 dynamic typing, 190 find command, 31 find feature, accessing, 64, 67 E find() string method, error related Easter egg, The Zen of Python as, 88 to, 178 encapsulation, explained, 307–308 finding programs, 35 encoding definition, using with magic first-class objects and callables, comment, 187–188 121–122. See also objects engine vs. library vs. framework vs.