Akshay πŸš€ Profile picture
Apr 13 β€’ 9 tweets β€’ 3 min read β€’ Read on X
LEGB rule in Python, clearly explained:
Every Python developer must know the LEGB rule!

It's crucial for avoiding major bugs!

And today, I will provide a clear explanation of the same!

Let's go! πŸš€ Image
Let's start by understanding meaning of scope❗️

The scope of a variable defines the area of a program from where you can access it.

The name will only be visible to and accessible by the code in its scope.

Here's an illustration of the same...πŸ‘‡ Image
1️⃣ Local Scope:

Local scope refers to variables defined within a function.

These variables are only accessible within the function where they are defined.

They are created at the function's call and destroyed when the function ends.

Example...πŸ‘‡ Image
2️⃣ Enclosing Scope:

Enclosing scope is relevant in the context of nested functions.

If a variable isn't in the local scope but is defined in an outer function, it's in the enclosing scope.

It is accessible from the inner function but not from the global scope!

Example...πŸ‘‡ Image
3️⃣ Global scope

Global scope encompasses variables defined at the top level of a script or a module.

Global variables are accessible from anywhere within the module or script, including inside any functions.

Created when the script starts and last until it ends.

Example...πŸ‘‡ Image
4️⃣ Built-in scope

Built-in scope includes names that are pre-defined in the Python language.

This scope contains functions like `print()`, ` len()`, and types like `int`, `float`, which are always available without the need for any imports.

Example ...πŸ‘‡ Image
What if scopes collide❓

When scopes collide in Python, the LEGB rule plays a crucial role in determining how variables are resolved.

Python always looks for a variable in the order:

Local ➝ Enclosing ➝ Global ➝ Builtin

Here's a good example: Image
That's a wrap!

If you interested in:

- Python 🐍
- ML/MLOps πŸ› 
- CV/NLP πŸ—£
- LLMs 🧠
- AI Engineering βš™οΈ

Find me β†’ @akshay_pachaar βœ”οΈ
Everyday, I share tutorials on above topics!

Cheers!

β€’ β€’ β€’

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

Keep Current with Akshay πŸš€

Akshay πŸš€ 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 @akshay_pachaar

Apr 10
Let's build a "Chat with your docs" RAG application, step-by-step:
Before we begin, take a look at what we're about to create!

We'll be using:

- @Cohere highly capable ⌘R+ as the LLM
- @Llama_Index for orchestration
- @Streamlit for the UI

Everything in just ~170 lines of Python code, that I've shared at the end! πŸ”₯

Let's go! πŸš€
The architecture diagram presented below illustrates some of the key components & how they interact with each other!

It will be followed by detailed descriptions & code for each component: Image
Read 11 tweets
Apr 9
Matplotlib subplots & subplot-mosaic clearly explained:
Today, we learn about:

- Regular subplots
- Subplot-mosaic πŸ”₯

Let's go! πŸš€ Image
1️⃣ Subplots

Check this out πŸ‘‡ Image
Read 6 tweets
Apr 8
5 GitHub repositories that will give you superpowers as an AI/ML Engineer:
1️⃣ Awesome Artificial Intelligence

A curated list of Artificial Intelligence:

- courses
- books
- video lectures
- and papers with code

Check this out πŸ‘‡
github.com/owainlewis/awe…
2️⃣ Cleanlab

You're missing out on a lot if you haven't started using Cleanlab yet!

Cleanlab helps you clean data and labels by automatically detecting issues in a ML dataset.

It's like a magic wand! πŸͺ„βœ¨

Check this outπŸ‘‡
github.com/cleanlab/clean…
Read 7 tweets
Apr 5
This is the future of building RAGs. Let me introduce you to DSPy today:
In terms of analogy, DSPy is to RAG as PyTorch is to DNNs.

DSPy : RAG :: PyTorch : DNNs

To understand DSPy, we need to grasp three key concepts:

- Signatures
- Modules
- Optimizers

Let's delve into each one! πŸš€ Image
1️⃣ Signature

We use a signature to tell DSPy what to do instead of how to do; they save us from writing gigantic & complex prompts.

DSPy supports inline short strings as signatures; however, you can always write a class for it if necessary! Image
Read 9 tweets
Apr 3
Tensors in PyTorch, clearly explained:
Tensors are the fundamental building blocks for performing mathematical operations in deep learning models.

Today, I will provide a comprehensive explanation with illustrative code examples.

Let's go! πŸš€ Image
Tensors are multi-dimensional arrays that form the backbone of numerical computing!

In PyTorch, creating tensors is a breeze!

You can initialize tensors from lists, zeros, ones, or even random values!

Check this outπŸ‘‡ Image
Read 7 tweets
Apr 1
7 projects that every AI engineer must explore:
1️⃣ Chat with your code using RAG

A step by step guide to building a RAG application using @llama_index!

Check this outπŸ‘‡
lightning.ai/lightning-ai/s…
2️⃣ Search images with words

Build a model for rapidly searching an image dataset with words. This flexible pipeline can easily be tailored to your own dataset.

Check this outπŸ‘‡
lightning.ai/lightning-ai/s…
Read 10 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!

:(