Rodrigo πŸπŸš€ Profile picture
Oct 9, 2021 β€’ 21 tweets β€’ 6 min read β€’ Read on X
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

May 18, 2023
I know `print` is the first Python 🐍 function you learned! πŸš€

And yet, you don't know this about `print` πŸ‘‡ Image
What you know for sure is that `print` will take an object and it will print it on the screen.

That's the basic functionality it provides: Image
Maybe you don't know that `print` can actually print multiple things!

If you pass multiple arguments, they all get printed: Image
Read 11 tweets
May 17, 2023
I'll tell you the story of a person that had the wrong name…

And how to prevent that in Python 🐍 with properties πŸš€.

πŸ‘‡ Image
John Doe was a regular guy and when he was born, he was inserted into the government's database of people.

They created a new `Person` and added John's details: Image
John never liked his name Doe, though.

So Joe decided to change his name to Smith.

And so he did.

He updated his last name, but the government `Person` STILL had the wrong name! Image
Read 10 tweets
May 14, 2023
Opening a file to read/write is a common task in Python 🐍.

Here is how to do it right! πŸš€

πŸ‘‡ Image
Python has a built-in `open` that takes a file path and opens that file.

Then, you have to specify whether you want to open the file to read, write, or append.

But this isn't half of the story! Image
The default behaviour is to open the file to read/write text.

This works well with TXT or CSV files, for instance.

If you need to open a file to read its binary contents, you can add a `"b"` to the mode: Image
Read 6 tweets
May 13, 2023
The Python 🐍 built-in `round` is great. πŸš€

Here are some tips on it. πŸ‘‡ Image
The purpose of `round` is to… round numbers!

It rounds numbers to the closest integer.

These are some simple examples: Image
However, if the number ends in `.5`, what is the closest integer?

In that case, `round` will choose the even number.

This means it may round up or down πŸ€ͺ

(In school, I was taught to round `.5` up… 🀷) Image
Read 6 tweets
May 12, 2023
Error handling in Python 🐍 made simple. πŸš€

πŸ‘‡ Image
The keyword `try` is used before code that might fail.

So, if you know something can raise an error, you can write it inside a `try` statement: Image
Now that the code is inside a `try` statement, you need to tell Python what error you want to handle, and how.

That's when the keyword `except` comes in! Image
Read 7 tweets
May 11, 2023
Every Python 🐍 programmer uses the ICPO rule. πŸš€

Even if you don't know what it is.

Let me tell you what the ICPO rule is and what it does πŸ‘‡ Image
The ICPO rule is what determines how attribute and method lookup work.

More precisely, it is the ICPO rule that tells Python where to look for attributes and methods in your objects.

This is relevant because everything in Python is an object.
Because everything is an object, that means Python is incredibly consistent in some things.

For example, did you know that everything has a type? Image
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!

:(