You can use Python 🐍 docstrings to provide usage examples of your functions!

That's useful to your users!

And did you know that you can use those examples to do small automatic tests on your functions? 😱

Let me break down how in a mini thread πŸ‘‡πŸ§΅ Image
First off, we define a simple function.

In this case, we just define a function to return the successor of an integer: Image
Then, we define a basic docstring for the function, in a single line.

However, we also add an example showing how using the function looks like.

Notice that the example looks like the REPL: Image
Now, we can turn that example into a small test as well!

We just need to import `doctest`, which is in the standard library.

We import it, and run `testmod`, to test the current "module": Image
If your code agrees with the examples you gave, no output is shown.

So, for the sake of demonstration, let's introduce a small bug in the code, with the wrong return value: Image
Now that there is a bug, the code no longer agrees with the example in the docstring!

When that is the case, we get some output saying something is wrong: Image
Pretty cool, right?!

This can be useful if you use it sparingly, with a couple of basic usage examples/small tests in your functions' docstrings.

For extensive testing of your code, I recommend using a testing framework!

By the way, which one do you use?
Was this thread useful? I hope so!

If you want to have some high-quality Python 🐍 content in your timeline, make sure to follow me @mathsppblog πŸ˜‰

Also, feel free to retweet the beginning of this thread to help your friends improve their Python πŸ’ͺ

I'm sorry, everyone.

A formatting issue kind of ruined the code. This is what I wanted to share:

Notice the REPL-like input with >>> and the (correct/expected) result in the next line. Image

β€’ β€’ β€’

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

13 Oct
Python 🐍 docstrings πŸ“ are very important, because they are the first level of documentation of your code.

Docstrings are great because IDEs can often display them, to give you help, when you start using a function.

Here's some advice on how to write them πŸ‘‡πŸ§΅
The first line of a docstring should be a short sentence explaining, in English, what the function does.

Write the docstring as a command, not as a long-winded description of what is going to happen.

Go straight to the point, make that line count!
If the function isn't trivial, you need to write a more complete docstring.

Explain what the arguments do and the return value.

Explain what/when the function raises errors.

Let the user know of any side-effects that might happen.

...

Write all of this after a blank line.
Read 4 tweets
13 Oct
🚩🐍 Here's a 4-step roadmap to mastering Python!

The roadmap will take you from complete beginner to building real-world Python apps πŸš€

We'll leverage β€œThe Indie Python Extravaganza”, a πŸ“š bundle that you can get for free during October πŸ‘‰ leanpub.com/b/theindiepyth…

πŸ‘‡πŸ§΅
1. If you are a Python newbie, then I recommend you start with β€œPython 101”, 2nd edition, by @driscollis.

This will teach you the fundamentals of Python!

But if you want to learn how to code, you need to write code!

Makes sense?
2. That's why you should also take a look at β€œPractice Python Projects” by @learn_byexample, which will have you write small projects.

This will be very important for you to develop a level of comfort with writing Python code!

Next up, you want to write the best code possible!
Read 8 tweets
9 Oct
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”.
Read 21 tweets
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

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!

:(