Rodrigo ๐Ÿ๐Ÿš€ Profile picture
Sep 29, 2021 โ€ข 28 tweets โ€ข 9 min read โ€ข Read on X
I challenged you ๐Ÿ†...

You delivered ๐Ÿ’ช!

I asked you to implement the sign function in Python ๐Ÿ.

Now I'll go over some alternatives and tell you what I like โœ… and dislike โŒ about them.

I'll also tell you which one I think is the best, most Pythonic โœจ one.

๐Ÿ‘‡๐Ÿงต
By the way, for reference, here is the original challenge:

Let's start with the โ€œbasicโ€ `if: ... elif: ... else: ...` approach first.

Gets the job done, is simple, and is easy to ready.

Funnily enough, the only one to share that solution was a _very_ seasoned Python ๐Ÿ programmer, author and trainer, @dabeaz ๐Ÿ™ƒ

Now, a couple of notes:
For the arg name, we don't need something VERY long like `number`, but don't shorten it to `n`.

`n` has a connotation for positive whole numbers, and `sign` accepts other numbers.

I also prefer to have the `else:` return the `0`, and that's to preserve the symmetry of 1 and -1.
We can use this as the cornerstone for many interesting implementations.

For such a simple function, it might look like a bummer that we have to spend 6 lines implementing it.

Can we shorten it a bit, perhaps?

Using conditional expressions, we can ๐Ÿ‘‡
We can also get rid of the `else`, which may reinforce the sense of โ€œ0 is just an edge-caseโ€...

Depending on how you usually read Python code.

Here it is:
Now, you might be thinking...

Instead of returning the conditional expression...

Can I compute ๐Ÿงฎ the 1 or -1?

Well, you can!

And many of you submitted things like this ๐Ÿ‘‡

But that gives rise to another issue...
Do we really need the `int`?

Well, depends on what you want your function to return, because `abs(x) // x` returns a float if `x` is a float.

And what about floating point inaccuracies? Does `int(abs(x) // x)` always return 1?
To be honest with you, I am not entirely sure!

I couldn't find a value of `x` for which `abs(x) // x` evaluated to 0.99999(something).

However, to be extra safe, you could replace `int` with `round`.

Ok, and are there โ€œsaferโ€ alternatives?
There are!

Have you heard about the Truthy and Falsy values of things in Python?

That makes it so that all objects can be interpreted as Booleans when needed.

But did you know that Booleans (`True` and `False`) can be handled as integers??

Hence, you can write this ๐Ÿ‘‡

Whatโ‰
I like the fact that it looks like a face:

(x > 0) - (x < 0)

The `-` is the nose, and the `(...)` are the eyes!

And there's even the added benefit that you can drop the `if` statement:
In my opinion, this is very cool!

BUT it's not the โ€œway to goโ€ in Python, generally.

This looks more like a thing you'd do in an array-oriented language, like APL.

So, we steered away from Pythonic solutions.

Can we get back on track?
Yes!

But let's not, for now ๐Ÿ˜‚

I just wanna show you another interesting one, making use of the integer value of Boolean values ๐Ÿ‘‡

It looks interesting, and it is useful to _understand_ how it works.

But I'm sure the majority won't feel this is Pythonic.
I think the conditional expression wasn't that bad, right?

Maybe we could improve on it?

Well, we can try!

What if we nest two conditional expressions to handle the three cases?

Again, just a quick remark about this one:
I like to have 0 at the end, because I like the symmetry between

๐Ÿ‘‰ x > 0 โ†’ 1
๐Ÿ‘‰ x < 0 โ†’ -1

This might sound silly to you, but these symmetries and patterns really make my life easier!

That's why I prefer this ordering over, say, this one ๐Ÿ‘‡
But we can still be friends if you order things your way ๐Ÿ˜

Another thing to notice is that all the `if`s with `if x == 0` could be replaced by `if not x`.

Some might prefer it, some might not.

I have no strong feelings for neither ๐Ÿคทโ€โ™‚๏ธ
Is there any other way to emulate the โ€œchoose one of -1, 0, or 1โ€ behaviour without using long `if`s..?

Someone submitted this, and I gotta say:

It looks odd, but it is incredibly easy to read.

Is it Pythonic? I don't think so ๐Ÿ˜ข
Instead of accessing a dictionary, we can also try to index into a list.

Now, close your eyes and skip this if you don't want to be horrified ๐Ÿ‘‡

It's not that bad ๐Ÿ˜‚

Again, not recommended style, but _understanding_ how it works gives insights into how Python works.
This can be taken one step further.

Should you?

Nah.

Will I do it nonetheless?

Absolutely!

Here it goes ๐Ÿ‘‡ Can you understand how it works?
Hmmm, 'kay.

That's all nice and fine.

What if I don't want to use `if`s, but also no weird Boolean computations?

Then, maybe you could try new pattern matching feature from Python 3.10!

In case you need to learn it:
Can we use `match` to solve this?

Well, not directly.

Which shows that `match` really isn't the tool for this job.

But we can use guards and get away with it ๐Ÿ‘‡

But if you look closely, the `case` statements aren't doing anything!

So, can we get rid of them..?
Absolutely, and you end up with this ๐Ÿ‘‡

Equivalent to the `if` from the beginning, but condensed.

Canned `if`-blocks, everyone!

Ok!

So, we used `match` and ended up getting away from it?

Unacceptable!

One more try:
`match` is supposed to be useful for structural pattern matching.

Can we match patterns, then?

Of course. Here's a solution no one submitted, but that I came up with ๐Ÿ‘‡

What do you make of it?
All in all, there are plenty of interesting solutions in this thread.

So, which ones do I prefer?

Well, *personally*, I love weird snippets of code ๐Ÿ˜‚ But I don't write them in production.

So, that means I end up with a split preference, between ๐Ÿ‘‡

and the slightly condensed `if: ... elif: ... else: ...` block ๐Ÿ‘‡

I don't know why, but it *really* bothers me that such a simple function takes 6 lines of code as a โ€œfullโ€ `if: ... elif: ... else: ...` block!

(Does anyone else feel like that?)

Thanks for making it so far!

I'd love if you read your thoughts on these solutions!

Here's a quick link to the beginning of the thread. Leave your comments there ๐Ÿ’ฌ and retweet ๐Ÿ” it if you found value in this thread!

Finally, if you have ideas for future challenges, feel free to send them to me!

Write them down in the comments, or send me a DM!

Then, if you want to keep learning a lot about Python ๐Ÿ, follow me @mathsppblog.

I'll see you soon! ๐Ÿ‘‹
P.S. I should give credit to everyone who contributed to this thread by sending their solutions.

Many people replied, so I don't think I should mention all of them..? You can find all of them in the original challenge, linked at the beginning.

Thanks a lot for participating!

โ€ข โ€ข โ€ข

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 9
Hide private information in your Python code.

Suppose you want to print an email...

But you want to create that cool ro*****@mathspp.com effect.

This is pretty easy to achieve in Python!

All you need to do is use an f-string and use the appropriate format specifiers. Diagram showing how you can use f-strings and their format specification to redact private or sensitive information, like email addresses.  The code from the diagram:  def redact_email(email):     user, _, domain = email.partition("@")     return f"{user[:2]:*<{len(user)}}@{domain}"  print(redact_email("rodrigo@mathspp.com")) # ro*****@mathspp.com
๐Ÿ‘‰ the first thing you do is use `str.partition` to grab the email โ€œuserโ€ and the domain.

We will redact only the user (but you could also redact the domain with the same process).

The `user[:2]` shows the first two characters.

That's the โ€œroโ€.

But how do you get โ€œro*****โ€?
๐Ÿ‘‰ use an f-string and the width specifier.

You want to create a field as wide as โ€œrodrigoโ€:

r o _ _ _ _ _

The length of this field is `len(user)`, so you use `{len(user)}` INSIDE the format spec.

This creates a field with the correct width.
Read 6 tweets
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

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!

:(