Mike Driscoll Profile picture
May 10 β€’ 9 tweets β€’ 5 min read
#Python comes with lots of modules. Let's get started by looking at the `abc` module.

The `abc` module isn't for learning the alphabet! It's for creating Abstract Base Classes 🀯!

Let's learn more about those now πŸ§΅πŸ‘‡

#PythonStdLib 🐍πŸ”₯
Abstract base classes (ABCs) are defined in PEP 1139.

You can read all about them here:

python.org/dev/peps/pep-3…
Abstract Base Classes in #Python provide the following:

🐍 A way to overload isinstance() and issubclass().
🐍 A new module abc which serves as an β€œABC support framework”.
🐍 Specific ABCs for containers and iterators, to be added to the collections module.
You can create a virtual class with `abc.ABCMeta` if you use it as the metaclass of a custom class.

Here is an interesting example:
The Python Module of the Week website has lots of interesting examples for Abstract Base Classes:

pymotw.com/3/abc/
A common use case for Abstract Base Classes to make a base class as a common interface.

This base class cannot be instantiated itself, but it can be sub-classed

If you forget to implement a method in your subclass, the subclass will raise an error.

Here are a couple examples:
You can read more on this topic in this excellent article by @dbader_org

dbader.org/blog/abstract-…
@dbader_org Don't forget to read the official #Python documentation to learn more about the `abc` module:

docs.python.org/3/library/abc.…
@dbader_org Thanks for checking out my article on #Python Abstract Base Modules. 🐍πŸ”₯

Follow me to learn more about the amazing Python programming language or check out my articles at @mousevspython

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

May 11
Module number 3️⃣ on our #Python standard lib tour is `argparse`

The argparse module makes it easy to write user-friendly command-line interfaces.

#PythonStdLib 🐍πŸ”₯

πŸ§΅πŸ‘‡
Here is an example of creating an argument parser with no arguments.

Why would you do that? Well, to find out what `argparse` gives you out of the box πŸ“¦!

You can see here that `argparse` gives you some help text and prints out your provided description Image
A common coding pattern for argument parsing is to put the argument parsing code into its own function or method and then return the parsed args.

Here's an example: Image
Read 8 tweets
May 11
The aifc module in #Python will be deprecated in Python 3.11

But it's still worth knowing about because so many people will still be able to use it.

This module provides support for reading and writing AIFF and AIFF-C files

docs.python.org/3/library/aifc…

#PythonStdLib 🐍πŸ”₯

πŸ§΅πŸ‘‡
AIFF is Audio Interchange File Format, a format for storing digital audio samples in a file. AIFF-C is a newer version of the format that includes the ability to compress audio data.

You can read and write these files with #Python using the aifc module!
When you go create an AIFF file with #Python, you use much the same type of code as you would when you use Python's `wav` module.

Here's an example: Image
Read 5 tweets
May 9
Did you know #Python has a `weakref` library built-in? 🐍πŸ”₯

Python won't delete an object until all its references are deleted. A weak reference does not protect you from garbage collection.

Join me for a short mini-thread!

πŸ§΅πŸ‘‡
Let's look at an example of #Python code that has two references to the same object

In this example, to be able to delete the object, you must delete both variables, "a" and "b": Image
Now let's use Python's `weakref` library to make the second reference into a weak reference

You'll note that now when you go to delete the original reference, the weak reference is also removed. Image
Read 6 tweets
Apr 28
#Python has TONS of great packages and frameworks.

In this thread, I am going to highlight just a few of the THOUSANDS that are available to you on pypi.org

πŸπŸ§΅πŸ‘‡
Python has lots of great cross-platform GUI packages:

🐍 PySimpleGUI @PySimpleGUI
🐍 PyQt / PySide
🐍 wxPython
🐍 Tkinter
🐍 @DearPyGui
🐍 EasyGUI
@PySimpleGUI @DearPyGui #Python also has a couple of GUI frameworks for mobile development (i.e. iOS and Android):

πŸ”₯ Kivy @kivyframework
🐍 Toga from @PyBeeWare

I have heard PyQt also supports mobile, but haven't been able to verify it
Read 10 tweets
Apr 21
Have you used #Python's `enum` module yet? No?

Then you should check out today's mini-thread!

🐍πŸ”₯πŸ§΅πŸ‘‡
Enum is short for enumerations which is a set of symbolic names (members) bound to unique, constant values.

Do NOT confuse the `enum` module with the `enumerate()` function though. 🐍⚠️

For full details on the `enum` module, see the docs:

docs.python.org/3/library/enum…
Let's look at an example of using the `enum` module.

In the following example, you subclass `enum.Enum`.

Then you add some enum values as class attributes, kind of like how you would with a dataclass

The `Enum` class supports iteration, so you iterate over it at the end
Read 7 tweets
Apr 20
#Python strings have TWELVE methods for testing string contents:

🐍 isalnum()
🐍 isalpha()
🐍 isascii()
🐍 isdecimal()
🐍 isdigit()
🐍 isidentifier()
🐍 islower()
🐍 isnumeric()
🐍 isprintable()
🐍 isspace()
🐍 istitle()
🐍 isupper()

πŸ§΅πŸ‘‡
Let's get started learning about these methods by looking at ` isalnum()`, which returns True if all characters in the string are alphanumeric and there is at least one character,
The `isalpha()` string method will return True if all characters in the string are alphabetic and there is at least one character
Read 13 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(