08 January 2013

The kind of story I tell my daughter...

A while back, I was trying to convince a certain child of mine that she really ought to sleep, no matter how much she wanted to stay awake. When I tried shushing her, she just screamed louder. But when I talked to her, she'd shut up, so she could hear what I was saying.

I should tell her a story, thinks I to myself.

Yeah? That'd be awesome, thinks my brain back to me. Only... I seem to have misplaced every children's story you've ever heard.

No worries, I'll just come up with a new one - she's not going to know the difference at this age, anyway, I think right back. I'll just tell her about something that would put most folks to sleep.

And this is what happened (with illustrations)...

How A and B together became us

Once, there were two variables, A and B. Now, normally, these variables would be defined in lower-case letters, but these are special variables, with their own personalities and likes and dislikes and favorite animals and everything. They're like people, like you and me, and A and B are their names. And as we all know, people's names are proper nouns, which are capitalized, so A and B are capitalized, too.

But that is not the story, dear one. This is the story of A and B and their quest to become more than two individual variables. Let us meet A and B now:

>>> A = 1
>>> B = 2

B was older than A, you see, but that's of no matter. A and B had been friends for a very long time, and they wanted to let everyone know how close they were, so they set off to search for a way to do just that.

"I think there's an operation for that!" said A one day. "Let's ask the operators if they can help!"

So A and B went to the doctor and asked about operations to show the world that they were as close as could be.

"Well, we could try the plus sign," said the doctor, and he brought one out on a surgical tray.

>>> A + B

"That's no good!" said B. "If we do that, we lose our identities and become a new value altogether."

"Yeah," A agreed. "We're both totally different, and we want to stay that way, but still let others see our relationship. Our relationship is more than the sum of its parts."

"Well," the doctor replied, "I don't think I have anything else here that will work for you. Subtraction, division, even multiplication - all of my operations produce a single result, without maintaining the components separately. You'll just have to try something else, I guess."

And so A and B left the doctor's office, feeling very sad.

The next day, B spent some time Googling, while A moped about. By mid-afternoon, an idea struck!

"I think I found it!" said B, running into the room. "Let's try a tuple! I just read online that they store multiple values at the same time!"

This cheered A right up, and that evening, they went out and met with tuple(), who looked very much like a comma in formal attire.

"I think I can help," offered tuple(). "Are you ready to try?"

"Sure!" said A.

"Of course!" said B.

"Then let's give it a go!"

>>> us = A, B
(1, 2)

"Awesome!" A and B said in unison.

"And you can see each of you individually, if you want," said tuple().

>>> us[0]

>>> us[1]

A was very excited. "Now people can refer to us as part of us, rather than calling us by name individually  Since we're together so often anyway, this will save a lot of time!"

"And when you're 2, too, A, we can just change your part of us!" said B.

"Whoa! Hold on there!" tuple() said. "That's not quite how it works. Once you're a tuple, that's immutable. You can't go changing parts of a tuple - you'd have to make the whole tuple over again!"

"That sounds like an awful lot of work," B replied.

"And I do so want to be 2 someday," said A, getting sad again.

But tuple() had an idea for the pair. "It sounds like I'm not going to be able to offer you what you're looking for," he began. "But my friend list() might be just the person for you to see. Here's his address."

And so A and B returned home once again, but this time, with hope.

The next day, they met with list() to ask if he could help them.

"We just really want to be part of the same variable," said A.

"But still be able to change our values," said B.

"Individually," said A.

Now, list() was a friendly guy, and he was pleased the two had come to see him. "I think I've got you covered," he said, stretching out his arms (which looked decidedly like square brackets. "C'mere and give me a hug."

>>> us = [A, B]
[1, 2]

"That's a good start," said B, "but can we still see our own values individually?"

"You sure can!" said list().

>>> us[0]

>>> us[1]

"Just like tuple()!" B proclaimed.

A was not entirely convinced yet, however. "What if one of us changes? Can we do that and still stay us?"

"Certainly," said the affable list(). "It's the simplest thing. In fact, how would you like to be 2 right now?"

"Oh, that would be lovely!" said the excited A.

"Well, you're part of us now, so we'll change your value there."

>>> us[0] = 2
>>> us
[2, 2]

"This doesn't change who you were as A, mind you. You're not A anymore. You're part of us."

"That's amazing!" us[0] (for he was, indeed, no longer A) crowed. "I feel like a new variable already!"

And so A and B became us, and us lived happily ever after.

No comments:

Post a Comment