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!
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.