Everyone knows that giving proper names to variables and functions is the true programmer's bane.

Here's a thread πŸ‘‡πŸ§΅ with do's and don'ts for naming things in Python 🐍

We can only hold a small number of simultaneous ideas in our 🧠, so proper naming can really help us!
There exist a bunch of naming conventions out there.

Some of them are more common in Python, and others are rarely seen in Python code.

Let me just show you what they look like, and then I'll walk you through when you could use each.
The PascalCase convention is often used with classes.

Notice that when you use PascalCase, you should uppercase all letters of an acronym πŸ‘‡

In the image, that's exemplified by the `AIPlayer`, where `AI` stands for β€œartificial intelligence”.
With the rise of the typing advent in Python, the PascalCase convention has also been widely adopted when naming type variables.

In hindsight, this aligns with the fact that PascalCase is used for classes.

After all, classes define new β€œtypes”!
PascalCase is great and all, but the bread and butter convention for Python is snake_case.

snake_case is the convention generally used for variables, functions, methods, ...
Another convention that shows up from time to time is the CAPS_LOCK_WITH_UNDERSCORES.

Naming things like this looks a little bit clunky, right?

Funnily enough, this is widely recognised as the convention for global constant variables!

In Python, and elsewhere!
After figuring out what naming convention to use, you need to figure out how verbose the names should be.

Should you write full sentences?

Abbreviate everything?

Use 1-letter names?
When figuring out how long names should be, consider two things:

πŸ‘‰ number of times you're going to use the name:

If a name is used very often, e.g. a function you call all the time, or a variable you access very often, that name can be shorter.
πŸ‘‰ distance between definition of the name and its usage:

If you define a name and only use it once in a completely different file, maybe that name should be more verbose.
As for the actual names you pick, there are also a couple of guidelines you can follow:

πŸ‘‰ always use the same spelling:
πŸ‘‰ always use the same vocabulary:
πŸ‘‰ use a name that reflects WHAT we are working with, not the type of data:
πŸ‘‰ prefer verbs for functions and nouns for variables:
Now that we've seen many tips, let's look at a case study.

Here's a function that was poorly named.

The function behaves like this:

>>> myfunc("abcdef")
'AbCdEf'
>>> myfunc("ABCDEF")
'AbCdEf'
>>> myfunc("A CDEF")
'A CdEf'
Take a moment to think about how you would improve the names that are used in this function.

Rework the function...

And now, let me show you what I would suggest πŸ‘‡
πŸ‘‰ the function name `alternate_casing` now indicates what it does to its argument;

πŸ‘‰ the parameter is now called `text` to help us understand what might get passed in as an argument;

πŸ‘‰ the list we are building is now called `letters`, to help us guess what is being appended.
πŸ‘‰ and a personal quirk of mine, I prefer to use `idx` as the name for an index variable to be extra explicit.

(For an added πŸ† challenge, you can try to refactor this function to improve it.

This is the function I used in my Pydon'ts talk πŸ‘‡)

I should say 2 more important things.

1️⃣ The context you are programming in also plays an important role in the names you pick.

For example, depending on whether you are writing code for a domain-specific package, some abbreviations may or may not make sense.
2️⃣ These are just guidelines, not RULES.

You should always use common sense to judge whether or not these guidelines make sense.

As an example, if you are contributing to a codebase, it's more important to follow that codebase's standards than to follow these guidelines.
That's it for this thread πŸ‘

If you are interested in Python 🐍, and knowledge-packed tweets and threads, follow me @mathsppblog for more πŸ˜‰

If this thread was useful to you, retweet it!

This thread was based off of an article I wrote a while ago on naming.

The article goes in more detail about some subtleties and includes more examples of what I talked about.

Check it out, and I'll see you around πŸ‘‹

mathspp.com/blog/pydonts/n…

β€’ β€’ β€’

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

Keep Current with Rodrigo πŸπŸ“

Rodrigo πŸπŸ“ 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 @mathsppblog

7 Oct
Python 🐍 dictionaries are amazing ✨ data structures.

Do you know how to make the best use out of them?

One method you should have in your arsenal is the `.get` method.

Here's a short thread πŸ‘‡πŸ§΅ about it.
A dictionary is a β€œmapping”: it maps keys into values.

In Python, if you have a key, you can use it inside `[]` to access the corresponding value πŸ‘‡

However, ...
... if the key doesn't exist, then you get an error!

As an example, here's my attempt at getting my age from the previous dictionary, which only knew about my name πŸ‘‡

So, how to try and access keys without having Python throw an error at your face..?
Read 12 tweets
6 Oct
πŸ†Β Here's a mini Python 🐍 challenge.

You have an int `value` and a dictionary `my_dict`.

`my_dict` **may** contain a key `"field"`, with an int.

If `"field"` is there, add it to `value`.

How would you solve this?

Here's a short thread πŸ‘‡πŸ§΅ with a couple of solutions.
First off, this mini thread was prompted by @svpino's most recent tweet, that I include here for credit and context:

Like Santiago pointed out, there are two archetypes of solutions here.

The first one, is to check if the `"field"` is there, and then add to `value`.

Simple to understand, but with one β€œdrawback” that bothers me just a little bit... Image
Read 11 tweets
4 Oct
What's the deal with Python 🐍's pass-by-value or pass-by-reference?

How does it relate to mutable and immutable objects?

Here's a Python thread πŸ‘‡πŸ§΅ that'll make everything crystal clear and help you master Python 🐍.

Let's go πŸš€
First thing you need to realise is that Python 🐍 does not use the pass-by-value model...

But it also does not use the pass-by-reference model!

Let me see if I can explain what I mean!

What does it mean to pass by value?
In the pass-by-value model, whenever you call a function with a given argument, that argument is fully copied into the function.

This means you can change what got into your function, and you won't be able to see the changes from the outside.

This _looks_ like pass-by-value:
Read 26 tweets
2 Oct
πŸ› πŸ‘€ On the 23/09 I challenged myself to go from 2.95k followers to 4k by the end of the month 😱

That was +1.05k followers (+36%) in 7 days.

On the 1st Oct I woke up to a little over 6k followers πŸŽ‰πŸ”₯ that's INSANE!

Thanks for your support!

Here's a recap of these days πŸ‘‡πŸ§΅
First off, for reference, here is the original tweet with the public challenge:

In the original tweet I said that my strategy would be to publish a high-value thread each day.

For my challenge to be met, I had to grow β‰ˆ4% each day.

Here's a breakdown of the number of followers I woke up to each day, with the % change relative to the previous day.
Read 13 tweets
2 Oct
I love the `enumerate` Python 🐍 built-in!

I love it because your `for` loops become much more readable ✨ if you know how to use it.

Here's a thread πŸ‘‡πŸ§΅ on how `enumerate` will make your Python code so much better.

Let's go πŸš€
Python's `for` loops are amazing!

And yet, many people write too much stuff in their loops.

For example, if you just need to go through a list, you shouldn't be writing this πŸ‘‡
In Python, you can just β€œgo through lists”, you don't need to compute the range of the length of the list!

Instead of the above, you can just write this πŸ‘‡

This is better because you can read it as β€œfor each colour in the list `l`”.
Read 16 tweets
30 Sep
β€œBe consistent”, they say.

But why?

Have you ever heard of the compounding effect?

Or exponential growth?

I'll show you how all these are connected, with real-life examples πŸ‘‡πŸ§΅ Image
I'll let you in on a little secret: I have a personal goal on Twitter.

The goal is to grow by 1% every day.

That's just that.

Every day, I want my follower count to increase by 1%

If you are anything like me, you might be thinking:
β€œIsn't 1% too little? You're gonna take ages to grow!”

Well...

Is 1% that little?

I'll show you it isn't!

First, here's a screenshot of my growth in the beginning of my Twitter journey πŸ‘‡

Let's work with those numbers... Image
Read 32 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

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!

Follow Us on Twitter!

:(