From 9eb06c36bd4320462ca04fd4872199e4ebd63684 Mon Sep 17 00:00:00 2001 From: d0vgan Date: Fri, 31 Mar 2023 18:33:15 +0300 Subject: [PATCH] some additional explanations --- Finished/Ch_1/bool.py | 17 +++++++++++++++++ Finished/Ch_2/docstrings_finished.py | 2 ++ Finished/Ch_2/keyargs_finished.py | 1 + Finished/Ch_3/listcomp_finished.py | 2 +- Finished/Ch_4/comparison_finished.py | 2 ++ Finished/Ch_4/compattrs_finished.py | 5 ++++- Finished/Ch_4/enums_finished.py | 1 + Finished/Ch_4/numeric_finished.py | 3 +++ Finished/Ch_5/simple_finished.py | 2 ++ 9 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 Finished/Ch_1/bool.py diff --git a/Finished/Ch_1/bool.py b/Finished/Ch_1/bool.py new file mode 100644 index 0000000..9c258ee --- /dev/null +++ b/Finished/Ch_1/bool.py @@ -0,0 +1,17 @@ +# demonstrates what is treated as False + +if not None: + print("None -> False") + +if not 0: + print("0 -> False") + +if not '': + print("'' -> False") + +if not []: + print("[] -> False") + +if not {}: + print("{} -> False") + diff --git a/Finished/Ch_2/docstrings_finished.py b/Finished/Ch_2/docstrings_finished.py index 570840e..abf110a 100644 --- a/Finished/Ch_2/docstrings_finished.py +++ b/Finished/Ch_2/docstrings_finished.py @@ -1,6 +1,8 @@ # Example file for Advanced Python: Language Features by Joe Marini # Demonstrate the use of lambda functions +# Refer to: +# https://peps.python.org/pep-0257 def my_function(arg1, arg2=None): """my_function(arg1, arg2=None) --> Doesn't really do anything special. diff --git a/Finished/Ch_2/keyargs_finished.py b/Finished/Ch_2/keyargs_finished.py index 694e6b4..e36aa9a 100644 --- a/Finished/Ch_2/keyargs_finished.py +++ b/Finished/Ch_2/keyargs_finished.py @@ -3,6 +3,7 @@ # use keyword-only arguments to help ensure code clarity +# the `*` parameter enforces the next parameter to be specified by the keyword def my_function(arg1, arg2, *, suppressExceptions=False): print(arg1, arg2, suppressExceptions) diff --git a/Finished/Ch_3/listcomp_finished.py b/Finished/Ch_3/listcomp_finished.py index 68f6595..0b5797a 100644 --- a/Finished/Ch_3/listcomp_finished.py +++ b/Finished/Ch_3/listcomp_finished.py @@ -11,7 +11,7 @@ map(lambda e: e**2, filter(lambda e: e > 4 and e < 16, evens))) print(evenSquared) -# Derive a new list of numbers frm a given list +# Derive a new list of numbers from a given list evenSquared = [e ** 2 for e in evens] print(evenSquared) diff --git a/Finished/Ch_4/comparison_finished.py b/Finished/Ch_4/comparison_finished.py index 495876d..71ca673 100644 --- a/Finished/Ch_4/comparison_finished.py +++ b/Finished/Ch_4/comparison_finished.py @@ -1,6 +1,8 @@ # Example file for Advanced Python: Language Features by Joe Marini # Use special methods to compare objects to each other +# Refer to: +# https://docs.python.org/3/reference/datamodel.html class Employee(): def __init__(self, fname, lname, level, years_service): diff --git a/Finished/Ch_4/compattrs_finished.py b/Finished/Ch_4/compattrs_finished.py index 3df08a1..8a89850 100644 --- a/Finished/Ch_4/compattrs_finished.py +++ b/Finished/Ch_4/compattrs_finished.py @@ -14,6 +14,8 @@ def __getattr__(self, attr): return (self.red, self.green, self.blue) elif attr == "hexcolor": return f"#{self.red:02x}{self.green:02x}{self.blue:02x}" + elif attr == "hex": # a method hex() + return lambda: f"#{self.red:02x}{self.green:02x}{self.blue:02x}" else: raise AttributeError(f"{attr} is not a valid attribute") @@ -28,7 +30,7 @@ def __setattr__(self, attr, val): # use dir to list the available properties def __dir__(self): - return ("rgbolor", "hexcolor") + return ("rgbcolor", "hexcolor") # create an instance of myColor @@ -36,6 +38,7 @@ def __dir__(self): # print the value of a computed attribute print(cls1.rgbcolor) print(cls1.hexcolor) +print(cls1.hex()) # set the value of a computed attribute cls1.rgbcolor = (125, 200, 86) diff --git a/Finished/Ch_4/enums_finished.py b/Finished/Ch_4/enums_finished.py index 7d88958..8ee08e2 100644 --- a/Finished/Ch_4/enums_finished.py +++ b/Finished/Ch_4/enums_finished.py @@ -28,3 +28,4 @@ class Fruit(Enum): myFruits = {} myFruits[Fruit.BANANA] = "Come Mr. Tally-man" print(myFruits[Fruit.BANANA]) +print(myFruits) diff --git a/Finished/Ch_4/numeric_finished.py b/Finished/Ch_4/numeric_finished.py index 82d4c97..d975e62 100644 --- a/Finished/Ch_4/numeric_finished.py +++ b/Finished/Ch_4/numeric_finished.py @@ -1,6 +1,9 @@ # Example file for Advanced Python: Language Features by Joe Marini # give objects number-like behavior +# Refer to: +# https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types + class Point(): def __init__(self, x, y): diff --git a/Finished/Ch_5/simple_finished.py b/Finished/Ch_5/simple_finished.py index 07b9b9c..83013a7 100644 --- a/Finished/Ch_5/simple_finished.py +++ b/Finished/Ch_5/simple_finished.py @@ -1,6 +1,8 @@ # Example file for Advanced Python: Language Features by Joe Marini # Simple pattern matching using literal values +# Note: match-case requires at least Python 3.10 + x = 0 # Literal patterns are explicit values like integers, strings, Booleans, etc