Discover and read the best of Twitter Threads about #python_builtins_by_driscollis

Most recents (17)

Starting in Python 3.7, the `breakpoint()` built-in function was added

It is defined in PEP 553 and simplifies adding a breakpoint to your code

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
You can read about the `breakpoint()` function for Python here:

python.org/dev/peps/pep-0…
Here is some sample code showing how to use the `breakpoint()` function in your Python code
Read 6 tweets
#Python includes a `format()` function that is built-in. This function is very similar to the string's `format()` method, but is more low-level than that method

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

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
Here are some examples of how you might use #Python's `format()` function: Image
The `format()` function uses the Format Specification Mini-Language. You can see this documented here:

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

You can use the Format Specification Mini-Language to specify alignment, fill, width, precision and more.

Here are a couple of examples: Image
Read 4 tweets
#Python includes several different ways to sort things. One handy way to sort is to use the built-in `sorted()` function!

Let's learn about that today!

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
The `sorted()` function takes in an iterable and a couple of optional arguments.

In this example, you pass in a Python list and sort it from smallest to largest.

Then you set the optional `reverse` argument to True and sort from largest to smallest! 🐍πŸ”₯
The `sorted()` function also takes an optional `key` parameter. This parameter is a function that is used to modify the sorting behavior.

In this example, you use `str.lower()` to sort the words of a sentence. It will compare all the words in lowercase while sorting.
Read 5 tweets
#Python includes two functions for creating sets. You are probably familiar with `set()`, so let's talk about the `frozenset()` built-in today! β˜ƒοΈπŸ

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

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
The `set()` data type is mutable, which means you can add and remove items. The `frozenset()` is immutable and hashable.

That means you can NOT change a `frozenset` after creation.

However, because it is immutable you can use them for dict keys or as an element in a set
Here are a couple of examples of creating a `frozenset()` in #Python
Read 3 tweets
One of #Python's many built-in functions is called `id()`. You can use `id()` to return the β€œidentity” of an object.

The id is an integer which is guaranteed to be unique and constant for this object during its lifetime.

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
CPython implementation detail for ids: This is the address of the object in memory.

Identity and equality are NOT the same in Python!

For example, the two lists in the following example equal each other (i.e. have the same contents), but their id is different!
In CPython, there is an optimization where the first 256 integers have the same identity

This can be proven with Python's `id()` function.

So in this case, the identity and the equality are the same
Read 5 tweets
#Python also includes a special function named `__import__()`

This function is NOT used in everyday Python programming and is in fact, usually discouraged.

But we will talk about it briefly anyway!

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
The `__import__()` function takes the following args:

🐍 name
🐍 globals=None
🐍 locals=None
🐍 fromlist=()
🐍 level=0
You can use `__import__()` to import modules. Here's an example of import Python's `sys` module using it:
Read 6 tweets
#Python includes a handy little function called `repr()`.

`repr()` will return a string containing a printable representation of an object.

Let's see how you might use it!

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
The `repr()` function will call the `__repr__` dunder method of a class.

This method returns a string that is primarily for developers and will never be seen by the end-user.

Let's see what happens when you call `repr()` on a class object without a `__repr__` method Image
Now let's add a `__init__()` and a `__repr__()` method to your class so you can modify what is returned so that it is more useful

Notice that now it returns what object type it is along with the name attribute. Before, all you got back was the memory address Image
Read 4 tweets
Even if you are a beginner, you have probably used #Python's built-in `print()` function.

But let's take a few minutes to talk about the humble `print()` function and see what you might have missed!

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
The `print()` function takes the following arguments:

🐍 *objects - Stuff to print
🐍 sep=' ' - A string to separate object
🐍 end='\n' - A string to end on
🐍 file=sys.stdout - Where to write the data
🐍 flush=False - Whether or not to force flush the stream
Let's look at some examples of using Python's `print()` function:
Read 5 tweets
Even if you are a beginner, you have probably used #Python's built-in `print()` function.

But let's take a few minutes to talk about the humble `print()` function and see what you might have missed!

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
The `print()` function takes the following arguments:

🐍 *objects - Stuff to print
🐍 sep=' ' - A string to separate object
🐍 end='\n' - A string to end on
🐍 file=sys.stdout - Where to write the data
🐍 flush=False - Whether or not to force flush the stream
Let's look at some examples of using Python's `print()` function:
Read 5 tweets
If you want to use #Python to read and write text or binary files, then you'll need to familiarize yourself with the built-in `open()` function!

Join me for this thread to learn more!

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
#Python's `open()` function actually has quite a few different parameters that you can use:
The most commonly used parameters you will use with #Python's `open()` function are:

🐍 file - The path to the file
🐍 mode - Read, write, binary, etc
🐍 encoding - What encoding the file is in
Read 8 tweets
We have already learned about the `bin()` and `hex()` functions. Today you will learn about #Python's handy `oct()` function!

`oct()` is used to convert an integer number to an octal string prefixed with β€œ0o”

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
Here are a couple of examples of using the `oct()` function with positive and negative numbers:
The #Python documentation has a nice example that shows how to format octal numbers using string formatting:
Read 4 tweets
Python includes a built-in named `object`. This built-in isn't a function. It's actually a class.

The `object` class is the base class of all Python classes. It has methods that are common to all instances of Python classes.

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
The following two examples show equivalent empty classes in Python.

They both subclass `object`: Image
You can verify that a class is a subclass of a specific class using Python's built-in `issubclass()`. Image
Read 4 tweets
#Python includes a `locals()` function that is built-in too.

It will update and return a dictionary representing the current local symbol table.

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
According to the #Python docs, "free variables are returned by locals() when it is called in function blocks, but not in class blocks."

Also note that at the module level, locals() and globals() are the same dictionary.
Here are a couple of examples of using Python's `locals()` function: Image
Read 5 tweets
One of #Python's most controversial built-in functions is `eval()`. The main reason it is controversial is that it can be used to execute arbitrary code.

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
The arguments for `eval()` are a string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can be any mapping object.

Here is an example:
You can use `eval()` to evaluate/execute the arbitrary code objects that you get when you run Python's `compile()` function.

The `eval()` built-in is closely related to the `exec()` built-in. `exec()` will take a code block while `eval()` only takes a single expression
Read 4 tweets
Python includes a built-in callable named `bytearray()`.

The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. They return a new array of bytes

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
A bytearray in #Python takes the following optional arguments:

🐍 source[optional]: Initializes the array of bytes
🐍 encoding[optional]: Encoding of the string
🐍 errors[optional]: Takes action when encoding fails
It is often useful to see an unfamiliar concept in action. Here are two examples of creating a bytearray using different encodings and printing them out Image
Read 5 tweets
Starting in Python 3.7, the `breakpoint()` built-in function was added

It is defined in PEP 553 and simplifies adding a breakpoint to your code

#python_builtins_by_driscollis

πŸ§΅πŸπŸ‘‡
You can read about the `breakpoint()` function for Python here:

python.org/dev/peps/pep-0…
Here is some sample code showing how to use the `breakpoint()` function in your Python code Image
Read 6 tweets
Starting in Python 3.6, you can create asynchronous comprehensions!

Come learn about them with me in this thread!

πŸ§΅πŸπŸ‘‡ #python_builtins_by_driscollis
Asynchronous comprehensions were defined in PEP 530.

You can check out the nitty gritty details here:

python.org/dev/peps/pep-0…
When you go look at that example, one of the first examples you will see looks like this the following

If you attempt to follow that syntax though, you'll end up with SyntaxError 🐍⚠️
Read 5 tweets

Related hashtags

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.00/month or $30.00/year) and get exclusive features!

Become Premium

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

Donate via Paypal Become our Patreon

Thank you for your support!