20
Python 2 in Python 3! (programming.dev)
submitted 1 year ago* (last edited 1 year ago) by qwop@programming.dev to c/python@programming.dev

A post about how this community's banner used the python 2 print syntax - print "Hello World" - made me question, can we print a hello world message in Python 3 without using parentheses?

It turned out to be sort of a fun challenge, I've found 3 different approaches that work. I'd be interested to see what you come up with! (it seems I can't put spoilers in Lemmy, so I won't share my solutions yet in case y'all want to have a go).

Edit: Posted my solutions in the comments

top 12 comments
sorted by: hot top controversial new old
[-] jsalvador@programming.dev 7 points 1 year ago

Just one question: why?

[-] sisyphean@programming.dev 5 points 1 year ago

You can put spoilers in posts or comments this way:

TitleSecret

Here is how it renders:

TitleSecret

(AFAIK apps don't render these correctly, only the website)

[-] nydas@lemmy.world 6 points 1 year ago

Can confirm not hiding on Memmy

[-] Pabo@feddit.nl 5 points 1 year ago

Same on Jerboa. Though given the apps' fast development progress we probably won't have to wait too long :)

[-] pythonoob@programming.dev 1 points 1 year ago

Not working on connect either

[-] jormaig@programming.dev 3 points 1 year ago

You could base64 encode the solution

[-] McCheng@lemmy.fmhy.ml 3 points 1 year ago

But then you still need to use parentheses to decode it

[-] jormaig@programming.dev 2 points 1 year ago

Ah sorry I meant for the spoiler. He could base64 the solution given that he doesn't know how to do spoilers on lemmy

[-] qwop@programming.dev 1 points 1 year ago

Ah true that would have worked, figured since spoilers don't work on most apps I might as well just post them.

[-] McCheng@lemmy.fmhy.ml 2 points 1 year ago

Gave some thoughts but couldn't find any. I need the solutions

[-] qwop@programming.dev 7 points 1 year ago

Alright, here are my solutions :)

  1. Import Easter egg
import __hello__

Not the most technically interesting, but a fun Easter egg!

  1. Class decorators
@print
@lambda _: "Hello World"
class Foo: 
    ...

Decorators are another way of calling a function, so can be abused to solve this task. You need to decorate a class rather than a function though, since a function definition requires parentheses!

  1. Dunder methods
class Printer:
    __class_getitem__ = print

Printer["Hello World"]

There might be some other Dunder methods you can use to do this, although it's sort of difficult since most (e.g. __add__) only describe behaviour on the instance of the class.

  1. More dunder methods
from _sitebuiltins import Quitter
Quitter.__add__ = print
exit + "Hello World"

Writing number 3 made me realise I just needed to find a class that already had an instance and change that. I'm sure there are many other cases of this, but here's one!

[-] McCheng@lemmy.fmhy.ml 1 points 1 year ago

Very interesting solutions. I was in the direction of using dunder methods but could not figure out how. Haven't heard of the __class_getitem__. The second solution is especially inspiring.

this post was submitted on 01 Jul 2023
20 points (88.5% liked)

Python

6416 readers
2 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS