marian
(marian K.)
March 27, 2024, 12:04pm
1
Did you know that checking for a type can actually be quite funny?
Because type(type) returns just ‘type class’, which can be chained further like:
type(type)('abc')
which returns final ‘str class’ of abc string.
But that’s not all, there are other ‘builtin’ things that when called in type() return ‘type class’. For example type(int) ; type(set) and even type(filter) ; type(zip) return just ‘type class’, which can be similarly chained like:
type(filter)('abc')
which returns final ‘str class’ of abc string.
So it is actually possible to write something like this:
type(bool)(memoryview)(bytearray)(bytes)(classmethod)(complex)(dict)(enumerate)(filter)(float)(frozenset)(property)(int)(list)(map)(object)(range)(reversed)(set)(slice)(staticmethod)(str)(super)(tuple)(type)(zip)('abc')
and still getting final ‘str class’ of abc string
What’s more, I’ve found out one can even encapsulate initial type within parenthesis like:
(type)(bool)(memoryview)(bytearray)(bytes)(classmethod)(complex)(dict)(enumerate)(filter)(float)(frozenset)(property)(int)(list)(map)(object)(range)(reversed)(set)(slice)(staticmethod)(str)(super)(tuple)(type)(zip)('abc')
which is similar to writing:
(print)('hello')
Yes, it’s valid! But that’s probably another story. Code is poetry!
Thanks for reading
MK
Next exercise: Write a quine function in Python. (Without using open or any io functions.) (The wiki article spoilers, so don’t read more than the first two paragraphs if you want to have fun with this.)
Python makes this super easy. But I feel like introspection and especially the inspect module, are not in the spirit of the original quine challenge.
import inspect
def quine_func():
""" Returns its own source code. """
current_frame = inspect.currentframe()
outer_frame_info = inspect.getouterframes(current_frame)[0]
this_funcs_name = outer_frame_info.function
this_func = current_frame.f_back.f_locals[this_funcs_name]
return inspect.getsource(this_func)
inspect.getsource uses open.
If using .pth files is allowed, one could install a codec that prints the
source code when it receives it for decoding.
# -*- coding: quine -*-
… but that feels like cheating, since it requires external code.
A fun (and not cheating) quine is this:
File "<path to file goes here>", line 1
File "<path to file goes here>", line 1
IndentationError: unexpected indent
When run, it outputs:
File "<path to file goes here>", line 1
File "<path to file goes here>", line 1
IndentationError: unexpected indent
(This is a quine for Python 3.12, but it won’t be a quine on all versions because traceback formatting
varies between versions.)
Very interesting. [edit] I see - hacking the error output mechanism. Genius! Props.
If we’re going to strictly enforce Hans’ requirements, then the challenge was a “quine function” too, not a quine program
That’s true. I don’t think there’s an easy way to get the error quine into a function, so I’ll have to give up on that for now, haha.
Rosuav
(Chris Angelico)
April 11, 2024, 6:34am
10
While we’re looking at trivial Python quines, here’s one:
(An empty program does nothing, and thus produces no output.)