Chapter 10: In which you Eval the Doggy

Start Here

Chapter 1: In which a cow surprises you 

Chapter 2: The Time traveling cow

Chapter 3: Greedy banker strings

Chapter 4: More on strings and donkeys

Chapter 5: Crooks R Us Bank & Lists

Chapter 6: Orc functions

Chapter 7: Dictionaries, Locks, and Gobbledygook

Chapter 8: Python vs the Zombies

Chapter 9: The Doggy-Nator and List Comprehensions

Chapter 10: In which you Eval the Doggy


The Great Battle between Good, Evil, and freshly grown Broccoli

There is a great battle raging between you and the Doggynator. With the BFG 9000 Elder Wand (Made in China), you can weaken the Doggy’s attacks, but he is too strong. He keeps attacking you, and to make it worse, he knows how to cancel your attacking spells.

Moo-Inator whispers something to you. “The only way to beat him is to use the magic words, ‘Ipso facto Deus Ex Machinia, Attack number 21!!'”.

You try to chant this powerful spell, but the Doggy-Inator won’t let you finish your sentences, as he cancels your attack. 21 is the secret number, the Dues Ex weakness you fond out using your special Chosen One powers. Here is a function to defeat the Doggy:

All you need to do is call the function super_attack with 21, and that will defeat the enemy. If only the Doggy-Inator would let you. The Doggy-Inator won’t allow you to say “Ipso facto Deus Ex Machinia, Attack number 21!” How about if we trick it?

The last topic we will study is another very powerful and cool one. Python allows you to create code in real time. Here’s how.

The eval is one of the coolest commands in Python, as it allows you to create a Python command at run time. You pass it a string, and eval will run it like a Python command.

Any Python command can be run in eval:

Obviously, this is a security risk, so don’t use it in sensitive production code. That said, I have found eval() very useful in internal test scripts, when I didn’t know which function I would have to call till run time. So I could create my own function, real time, and call it via eval.

Anyway, we will assemble our secret attack in (no surprise), secret.

You may not understand what happened above, but don’t worry, I will go over the details.

Okay, some recap. As you may remember, we can add strings with a +.

The only other thing you may not know is the str() function. It takes a number, and converts it to a string.

So we declare five strings: a,b,c,d,e and hide our super secret function in them. So when we do secret_command = a + b +c +d +e, secret_command now contains super_attack(21).

But fat use that is to us. That just prints the command on the screen, it doesn’t call it!

So how do we call it? If we try to call it directly, the Doggy-Inator cancels our attack? We use eval()

And there we go! Doggy-Nator defeated.

The Doggy-Nator falls into the volcano. You throw in the One Laptop too.

“It’s all over.”

“No, it isn’t. There is one more chip.” Moo-Inator points at his head. “It too must be destroyed. I cannot self destruct. You must lower me into the fire.”

You slow lower Moo-Inator into the fire, tears flowing, while emotional techno music is playing in the background. The Moo-Inator waves you a final goodbye, before being burnt in the flames of Eyjafjallajökull. And then you make a boring rambling speech about the road home being too long, and not enough coffee breaks on the way.

Just kidding. You don’t do any of that. It’s happy hour, and you have to go drinking with your friends (well, your one imaginary friend). So you push Moo-Inator into the volcano, and run to the bar.

The End

Leave a Reply

Your email address will not be published. Required fields are marked *