Let's type type type ... yes it's a valid python code :)

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 :slight_smile:


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! :slight_smile: But that’s probably another story. Code is poetry!

Thanks for reading
MK

7 Likes

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.)

1 Like

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)

1 Like

Very true :slight_smile:

1 Like

inspect.getsource uses open.

2 Likes

Drat. You’re right.

1 Like

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.)

3 Likes

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 :wink:

1 Like

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.

1 Like

While we’re looking at trivial Python quines, here’s one:

(An empty program does nothing, and thus produces no output.)

1 Like