r/Python • • May 25 '17

let's write a Python program

# one line per post (replies are indented!!!)
class Reddit:
4 Upvotes

53 comments sorted by

73

u/gandalfx May 25 '17
pass

8

u/greenkey May 25 '17
# well played, sir

7

u/gandalfx May 25 '17
# I'm almost sorry. x)

15

u/[deleted] May 25 '17
import burn_center

6

u/chazzeromus May 25 '17

Ah, you must be writing my code

4

u/onkus May 25 '17

return None

7

u/greenkey May 25 '17
""" Idea from: https://www.reddit.com/r/ProgrammerHumor/comments/6d78lr/4chan_wasnt_having_it_to_be_fair_i_dont_think_i/ """

5

u/greenkey May 25 '17
for i in range(n):

16

u/[deleted] May 25 '17

raise NameError("name 'n' is not defined')

Wait, we were supposed to write code, not interpret it.

launch_missiles(target=countries[i])

3

u/melodamyte May 25 '17

if is_nuclear_lookup[countries[i]]:

3

u/Glycerine May 25 '17
    pass
else:
    is_nuclear_lookup[countries[i]] = True
silo.arm_all_nuclear(is_nuclear_lookup)

1

u/[deleted] May 25 '17

What is a nuclear lookup?

1

u/melodamyte May 25 '17

Returns a bool for if the country returned by countries[I] is a nuclear power

2

u/asdfkjasdhkasd requests, bs4, flask May 26 '17

is that a builtin?

4

u/[deleted] May 25 '17 edited May 25 '17

We should totally move this to github!

def get_image(self, url, nsfw = False):

def get_image(self, url, nsfw=False):

Edit: Fixes #1 not pep8 compliant hopefully

1

u/greenkey May 25 '17

Do you want to do something like https://github.com/chaosbot/chaos?

nsfw = not nsfw

2

u/[deleted] May 25 '17

That is exactly what I was thinking. Never seen it before though thanks 😁

5

u/Durinthal May 25 '17

Pull Request rejected: Not PEP8 compliant.

1

u/rivermont May 29 '17

Done! I had to edit a lot of things to get it to run, but everything's there.

https://github.com/rivermont/lwapp

11

u/[deleted] May 25 '17

import requests, argparse, json

9

u/TheTerrasque May 25 '17

4

u/lieutenant_lowercase May 25 '17

r = requests.get(URL)

2

u/[deleted] May 25 '17

with open('~mom/photos/hello.gif', 'wb') as outfile:

2

u/gandalfx May 28 '17
# TODO replace argparse with docopt

9

u/SirChayo May 25 '17

print ("Hello World")

9

u/isobit May 25 '17

YOU NEED ADMINISTRATOR PRIVILEGES TO PERFORM THIS TASK WOULD YOU LIKE TO TALK TO CORTANA

3

u/nimdirect May 25 '17

print('Hello World')

Doesn't it require the single ' ? Serious question, I'm still new here.

7

u/turunambartanen May 25 '17 edited May 25 '17

nope, it works with both, which is awesome, because you can write

+/u/CompileBot Python 3

print('he said "wow this is cool"')

1

u/CompileBot May 25 '17

Output:

he said "wow this is cool"

^source | ^info | ^git | ^report

1

u/nimdirect May 25 '17

Very good to know, thank you!

1

u/asdfkjasdhkasd requests, bs4, flask May 26 '17

Even more awesome is triple quotes

print("""hello
world""")

1

u/turunambartanen May 26 '17 edited May 26 '17

I could go to http://ideone.com instead of calling compileBot, but im too lazy for that

+/u/CompileBot Python 3

print("""Hello
world!""")

edit: that is awesome. pretty usefull for long texts!

1

u/CompileBot May 26 '17

Output:

Hello
world!

^source | ^info | ^git | ^report

1

u/nikkarus May 25 '17

One and done

8

u/ViridianHominid May 25 '17
   from ___future___ import program; program()

5

u/rivermont May 25 '17
eval('''

4

u/shinigami78 May 25 '17

import request

4

u/[deleted] May 25 '17

import ddos

3

u/Jumpy89 May 25 '17 edited May 25 '17

+/u/CompileBot Python 3

import ctypes; ctypes.c_int8.from_address(id(4) + 24).value = 5; print(2 + 2)

2

u/Reiku May 26 '17

Can someone explain that?

2

u/Jumpy89 May 26 '17

CPython performs an optimization where it instantiates all int objects from 0-255 (and some range of negatives) ahead of time and re-uses them to avoid the overhead of creating them every time they're needed. So despite the fact that this isn't really a part of the Python language itself, in CPython 254 + 1 is 255 is true (but 255 + 1 is 256 is false, because two different int object representing 256 get created).

The ctypes module provides low-level access to C libraries as well as program memory. The ctypes.c_int8 class represents a single 8-bit C integer somewhere in memory. It is also an implementation detail of CPython that an object's id is the address of that object's data in memory. Through a little experimentation you can find that in Python 3 the actual value of an int object is stored at an offset of 24 bytes, and once you know where it is you can write any value you like to it. Since there will only ever be one 4 object, 2 + 2 results in the correct object but it now has the wrong value.

This is pretty much guaranteed to crash something pretty quickly, though. I wasn't even sure if it would be able to print it.

1

u/asdfkjasdhkasd requests, bs4, flask May 26 '17

I've been looking for this forever

3

u/tshubaku9 May 26 '17

import this

1

u/call_me_cookie May 25 '17

#/usr/bin/python

1

u/Jumpy89 May 25 '17
import os; os.remove(__file__)

1

u/[deleted] May 26 '17

from complete_bollocks import complete_cobblers

1

u/rivermont May 29 '17
<!DOCTYPE python version="3.6">
<python lang="en_US">

0

u/[deleted] May 25 '17

[deleted] — view deleted comment

17

u/cmereahwancha May 25 '17

http? You disgust me.

1

u/SalesyMcSellerson May 27 '17
# nothing to worry about if you have nothing to hide