Mike Driscoll Profile picture
Feb 10 โ€ข 5 tweets โ€ข 3 min read
#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
The `locals()` function is closely related to #Python's `globals()` function.

I talked about that last month. You can check out that thread here:

Thanks for learning about Python's handy `locals()` function with me today!

Check out my other threads for more great #Python content or check out my website @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

Feb 12
It's the weekend, so now's a great time to talk about #Python and SQLite! ๐Ÿ

Python works with SQLite, a file-based database, right out of the box via its built-in `sqlite3` library.

Here's how you could connect to a SQLite database with Python: Image
When you use the `sqlite3` module in Python, you will be using regular SQL to work with it.

So to create a table, you would do something like this: Image
The following code shows how to insert one row of data into a table as well as how to insert multiple rows using SQL and sqlite3 in Python ๐Ÿ๐Ÿ”ฅ Image
Read 8 tweets
Feb 10
Did you know that #Python has limited support for function overloading? ๐Ÿ๐Ÿ”ฅ

You get access to function overloading through `functools.singledispatch`

Here is an example: Image
You can also register multiple types to the same function when overloading functions in #Python

Here's how: Image
You can learn more about using function overloading in #Python in this tutorial of mine @mousevspython

blog.pythonlibrary.org/2016/02/23/pytโ€ฆ
Read 4 tweets
Jan 19
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
Jan 9
Let's talk a little about accessing #Python dictionaries!

Most beginners will access a dictionary like this:

๐Ÿงต๐Ÿ๐Ÿ‘‡
What happens if you try to access a key that doesn't exist in a #Python dictionary?

You'll get a KeyError!
There are several different ways to handle a KeyError. ๐Ÿโš ๏ธ

You could wrap it in a `try/except`. Another approach is to use the `get()` dictionary method, like this:
Read 6 tweets
Jan 7
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
Jan 7
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

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!

:(