Show newer

@Crocmagnon it’s a thread so you should be able to see the rest once you expand/click on the post

As of Python 3.10, all the binary search helpers in the bisect module now accept a key argument.

Read the full article: What's great about Python 3.10?
trey.io/ci2i5f

#Python

Here's an example:

You have a number of teams of people:

`teams = [["Bob", "Mary"], ["James", "Kate"], ["Bob", "Mary"]]`

Unfortunately, Bob and Mary are common names and there are two teams that each have a Bob and a Mary. I know, the coincidence of that!

`>>> teams[0] == teams[2]`
`True`

But, they're not the same team! They're different people!

`>>> teams[0] is teams[2]`
`False`

If you want to know whether these are the same teams, you need \`is\' in this case

@trinsec It's a lot more likely to need `==`, but the use of `is` does come about when you need to check that two objects are the exact same ones. It's not that uncommon.

Also, some prefer to use `is` with singletons such as `None`

`if stuff is None:`

Although in this case, there's no difference between `==` and `is` as there is only one `None`

But, there are times when you want to know that an object is exactly the same object and not just equal in value.

In these cases, you'll need to use \`is\`.

As a general rule, use \`==\` by default unless you know you need \`is\`.

But now can we stop squabbling about who the £5 note belongs to?!

/5

Show thread

Sorry, the serial number of the note shows it’s \__mine_\_!

This \***is**\* my note. In this case, it wasn’t \`==\` that matters but \`is\`

With a object, the built-in \`id()\` function gives you the "serial number", which is unique for each object, just as the serial number on a bank note is unique.

In , as in the case with bank notes, you often care more about the value of an object rather than its identity.

So, you'll often need \`==\` just like the barista in the coffee shop didn't care about the £5 note's serial number.

/4

Show thread

Its value is no longer what matters now.

We both had a £5 note.

What matters is whether this is the actual physical note that was in my pocket or yours.

Luckily, we're both geeks and we keep a record of the serial numbers of all the notes we carry with us on our phone.

I know, that's quite sad.

And quite unlikely, too.

But bear with me.

So, we both get our phones out to check the serial numbers on record against the serial number on the £5 note on the floor.

You can see the serial number in two places on the £5 note.

/3

Show thread

So, does it matter which £5 note we use to pay?

No, all the barista cares about is that \`our_payment == 5\`

So \__you_\_ can pay. Thank you!

In this case, the \***value**\* of the £5 note is what matters, not which actual physical note we use.

But let's assume we're chatting and we spot a £5 note on the floor.

We both check our pockets and we realise we've both lost our £5 note.

Is the note on the floor mine or yours?

/2

Show thread

Have you heard the one about \`is\`, \`==\`, and the £5 note that it's your pocket?

You may have already come across the fact that \`is\` and \`==\` are not the same in Python and cannot be used interchangeably… if you haven't, you heard it now!

But why? Let's see what the £5 note has got to do with this story

Let's assume you and I both have a £5 note in our pocket

_[replace with currency of choice]_
and
_[read "bill" instead of "note" if you wish]_

We meet up and go to a coffee shop nearby to buy two coffees which cost £5 altogether

Does it matter which £5 note we use to pay?

/1

_[this is like a Netflix series you can binge on, no need to wait until next week for the next episode, read on in the next toot in this thread…]_

@akhmxt Good luck, and there will be many of us here who can help, too. Just ask!

@DruidSmith @mmdolbow @GeoDon Yep, I think we all have our favourites! It doesn't really matter, is my mantra!

@GeoDon If you're unhappy with your IDEs, by all means do try others (PyCharm and VS Code the most popular these days), but don't let anyone tell you you must use XYZ, as they're just suggesting their IDE, not the best one!

@mapperwocky @GeoDon There's no best IDE/editor, there's only the best IDE/editor for **you**!

@GeoDon My answer to all IDE questions is, do whatever you're more comfortable with!

What Python IDE do you use?

@folkerschamel Some may say it's deliberate flexibility, but I think the trend is to use each one more consistently. But yes, there are many exceptions out there, including in the docs!

question: What are the pros of autodeleting toots? Only people who browse you profile will ever see them after the initial posting and any boosts, so why not leave them there?

Later this week, I'll look at other aspects relating to that can confuse those beginners moving on to more intermediate levels, including:

• _positional and named arguments_
• _optional arguments_ (by defining default values)
• the "obscure" _args and kwargs_
• forcing _positional-only_ or _keyword-only_ arguments using the "rogue" forward slash `/` and asterisk `*` in the function signature
• _type-hinting_ when defining functions
• some best practices when defining and using functions

Some of these topics I've written about a while ago in this Real Python article: realpython.com/python-optional

Show thread

"What's in a name?", one might argue? And there's a point. What matters most is knowing how to use concepts rather than knowing the precise definitions

Still, these terms are used everywhere-in documentation, in tutorials, when talking to other programmers

So, knowing the terminology _is_ important. It shouldn't be the first thing someone learns, but eventually, everyone should become familiar with the right terms for the right things

/6

Show thread
Show older
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.