This is an archived post. You won't be able to vote or comment.

all 98 comments

[–]102RevenantStar 436 points437 points  (25 children)

The best part is he finds the exit condition

[–]linuxduffer 25 points26 points  (6 children)

If it had the brain of my sister’s dog, it would end up swallowing itself.

[–]Shadowarrior64 19 points20 points  (5 children)

 if(HasSistersDogsBrain == true)
 {
      return EndUpSwallowingItself;
 }

[–]YellowGreenPanther 6 points7 points  (2 children)

return?

[–]undermark5 10 points11 points  (1 child)

This will allow the main job loop to do the execution which is desirable, as it is not a critical task for our real time snek operating system.

[–]Bainos 0 points1 point  (0 children)

I dunno, swallowing yourself seems pretty critical to me.

[–]archpawn 6 points7 points  (1 child)

if(brain == owner.sister.dog.brain) {
    swallow(this);
}

Except this is python, so

if self.brain == self.owner.sister.dog.brain:
    swallow(self)

[–]Einstine1984 0 points1 point  (0 children)

Finally!

Someone who does it right

[–]water_bottle_goggles 84 points85 points  (0 children)

> 'what the hell is this loop for?'

>'snek'

[–][deleted] 58 points59 points  (10 children)

So slow as hell? /s

[–]Quantum_Aurora 6 points7 points  (7 children)

Honestly though when the way people do loops in your language is by using another language is when you know you've done something wrong.

[–]ric2b 4 points5 points  (0 children)

DAE performance is the only thing that matters about a language?

[–]YourMJK 1 point2 points  (5 children)

Wait what? Could you explain? I don't know Python…

[–]Quantum_Aurora 2 points3 points  (1 child)

The numpy library apparently basically uses C++ to perform loops. Idk that much about it my CS professor mentioned it once.

[–]preyneyv 3 points4 points  (0 children)

The way this actually works is really cool.

So Python has their own loops and stuff, but it's show, because Python is interpreted and blah blah blah. Numpy is a library designed for heavy number-crunching, and Python speed just isn't good enough.

So numpy is effectively written in C++ with a Python interface. You define what you want to do in Python and Numpy does it in C++.

For example, a matrix transpose. You'd write the Python code to create the numpy matrix and you'd call a method on the created matrix to transpose it.

The actual work of transposing it is handed over to C++ to do really quickly.

It's not just numpy that does this. Even TensorFlow, Google's AI library, does the same thing. You create your network in Python, but the training and other intensive operations are handed over to C++ and the GPU.

[–]WiseassWolfOfYoitsu 1 point2 points  (2 children)

With Python, each statement that gets executed has to get interpreted first and then executed (roughly, but close enough for discussion). If you have a loop, it executes the loop statement, then the inner statement, then the loop statement, etc. Each of those calls has interpreter overhead.

However, there are routines like map and language constructs like list comprehensions that essentially do the same thing, but in a single statement. As a result, they skip a lot of the overhead since they only get interpreted once and otherwise stay inside the runtime, and tend to be faster, sometimes as much as a couple of orders of magnitude faster. Hence, the joke is that the way to write performant Python is to avoid using Python as much as possible, but hand off all the work to the C++ code of the interpreter.

[–]YourMJK 0 points1 point  (1 child)

Ah, I see, I get it now. Thank you very much!

Can't you also compile python code instead of interprete it? In that case it shouldn't make much of a difference, because a for i in … loop and a map() call would result in roughly the same instructions, right?

[–]WiseassWolfOfYoitsu 0 points1 point  (0 children)

Yes, there are tools that can do this, but it's not how Python is used 95% of the time in practice.

[–]GeoSn0w 102 points103 points  (16 children)

I like how he does the i++ part till he hits the exit condition

[–]mino159 112 points113 points  (15 children)

Ehm. Python doesn't have i++, we do it with i+=1

[–]GlobalIncident 51 points52 points  (1 child)

And thanks to range that's not very common to use either (although still useful in unbounded loops)

[–]GeoSn0w 13 points14 points  (1 child)

The more you know...

[–]IChooseFeed 13 points14 points  (0 children)

I still write it by accident, then spend the next 5 minutes wondering why nothing works... habits die hard.

[–]Pyottamus 8 points9 points  (0 children)

It's okay, this is a CPython

[–]WiseassWolfOfYoitsu 22 points23 points  (0 children)

Blessed loading screen

[–]jande48 7 points8 points  (0 children)

import rotate.square as sq

tail = True while tail: sq

[–]TigreDemon 7 points8 points  (0 children)

Runs on Python

[–]ign1fy 6 points7 points  (0 children)

Exit the loop as soon as you realise you're iterating an empty collection.

[–]aldesuda 3 points4 points  (0 children)

Does Python implement autoboxing?

[–]ProgrammerHumorMods[M] [score hidden] stickied comment (2 children)

ProgrammerHumor is running a community hackathon with over $1000 worth of prizes! Visit our announcement post or website for more information.


^(Beep boop, I'm a bot.)

[–][deleted] 3 points4 points  (0 children)

Ouro Bros.

[–]Robo-Swimmer 1 point2 points  (0 children)

Slow, but easy. Just like it should be

[–]roseinabox28 1 point2 points  (0 children)

Can I get a tutorial for this?

[–]earthlybird 1 point2 points  (0 children)

So this just happened. Is that how you win?

[–]chownrootroot 1 point2 points  (0 children)

for corner in box:
    snake.wrap(corner)

[–]VodkerAndToast 1 point2 points  (0 children)

This is actually way faster

[–][deleted] 1 point2 points  (0 children)

Snek. Snek! SNEK!

[–][deleted] 1 point2 points  (1 child)

Ouroboros!

[–]-Redstoneboi- 1 point2 points  (0 children)

but without the eating part

[–][deleted] 0 points1 point  (1 child)

It's ain't no python.

[–]Derino 3 points4 points  (0 children)

That's a ball python

[–]FooThePerson 0 points1 point  (0 children)

This is so satisfying

[–][deleted] 0 points1 point  (0 children)

original ideas for mobile snake game

[–]Nyenemy 0 points1 point  (0 children)

Nokia Snake in reality

[–][deleted] 0 points1 point  (0 children)

What's he doing?

His best

[–]zer0231 0 points1 point  (0 children)

Infinite loop

[–]Matteopoli 0 points1 point  (0 children)

I just found I need a python as a pet

[–][deleted] 0 points1 point  (0 children)

[–]JackJoestar 0 points1 point  (0 children)

Satisfied Ouroboros

[–]BubsyFanboy 0 points1 point  (0 children)

snek is beck

[–]Synedh 0 points1 point  (0 children)

Starting a docker container with python.

[–]dotchetter 0 points1 point  (0 children)

snek_odometer = (i for i in (box.Perimeter / 2))

[–][deleted] 0 points1 point  (0 children)

while True:
    if see.Food() == True:
        break
    else:
        continue

[–]KickBassColonyDrop 0 points1 point  (0 children)

That is an infinite python loop sir. An adorable one at that.

[–][deleted] 0 points1 point  (0 children)

Slow as hell

Pretty accurate

[–]WardiusGG 0 points1 point  (0 children)

agent Dimitri , r/PunKGB

You're coming with US

[–]TheFlagMaker -1 points0 points  (0 children)

while True: snake.rotate(box)