- Subclass (child)
- Superclass (parent)
- Subclass inherits from superclass
- First line in subclass constructor
super().__init__(args...)
\ - Method overriding
- To add specifics relating to the subclass(es)
any([...])
returns True if at least one is Trueall([...])
returns False if at least one is False*
wildcard number of argsfn(*args)
,args
will be a tuple
**
wildcard number of keyword argsfn(**kwargs)
,kwargs
will be a dict- Key is the keyword
- Value is the argument value
- Forcing keyword / positional args
fn(a, *, b)
forcesb
to be a keyword argfn(a, /, b)
forcesa
to be a positional arg
- Only have one expression, no statements
- Sorting
- Use tuples to sorting hierarchy
- Functions that take functions are arguments
pickle
module, serialises and deserialises data into binary files- Warning
- Only specific to python
- Same python version across pickles
- Don't unpickle files from untrusted sources
- CSV is a plain text file
- Structures data in a tabular form
- Default delimiter:
,
- Other delimiters:
\t
,:
,;
- Other delimiters:
- Other modules
xml.etree.ElemenTree
, creating and reading XML fileswave
andaifc
, reading and writing audio filestarfile
, reading and writing tar archiveszipfile
, working with zip archivesconfigparser
, creating and parsing configuration filesPyYAML
, process YAMLPyPDF2
, PDF toolkitPillow
, image reading and manipulation
unittest
built-in unit testing module- Create test class extending
unittest.TestCase
- All test functions start with
test_
main()
function runs all tests-v
verbose flag allows for more error logs
- All test functions start with
- Custom assertions
.assertEqual()
.assertTrue()
.assertIsInstance()
.assertIn()
.assertIsNone()
.assertLess()
.assertAlmostEqual()
- Testing fixtures
- Setup instance of object
def setUp(self)
- Teardown after test
def tearDown(self)
- Reusable
- Setup instance of object
- Other testing modules
pytest
doctest
Exception
is a superclass of all other exceptions- Having
Exception
has the firstexcept
clause will forbid any of the other clauses to fire
- Having
- Raising custom exceptions
class CustomException(Exception)