r/Python • u/greenkey • May 25 '17
let's write a Python program
# one line per post (replies are indented!!!)
class Reddit: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
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
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
4
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 nsfw2
5
1
u/rivermont May 29 '17
Done! I had to edit a lot of things to get it to run, but everything's there.
11
May 25 '17
import requests, argparse, json
9
u/TheTerrasque May 25 '17
URL = "http://pornhub.com"
4
2
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
1
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
1
8
5
4
4
3
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
intobjects 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 CPython254 + 1 is 255is true (but255 + 1 is 256is false, because two differentintobject representing 256 get created).The
ctypesmodule provides low-level access to C libraries as well as program memory. Thectypes.c_int8class 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 anintobject 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 + 2results 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
1
3
1
1
1
1
1
1
0
73
u/gandalfx May 25 '17