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

**URL:** <https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639>\
**Category:** Python Help\
**Created:** [March 27, 2024, 12:04pm UTC](https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639 "2024-03-27T12:04:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![marian](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@marian](https://discuss.python.org/u/marian)\
**Post date:** [March 27, 2024, 12:04pm UTC](https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639/1 "2024-03-27T12:04:54Z")

</div>

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:

```python
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:

```python
type(filter)('abc')

```

which returns final ‘str class’ of abc string.  
  
  
So it is actually possible to write something like this:

```python
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:

```python
(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:

```python
(print)('hello')

```

Yes, it’s valid! 🙂 But that’s probably another story. Code is poetry!

Thanks for reading  
MK

---

<div class="post-metadata">

**Author:** ![hansgeunsmeyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hansgeunsmeyer/32/15188_2.png) [@hansgeunsmeyer](https://discuss.python.org/u/hansgeunsmeyer)\
**Post date:** [March 27, 2024, 1:21pm UTC](https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639/2 "2024-03-27T13:21:42Z")

</div>

Next exercise: Write a [quine](https://en.wikipedia.org/wiki/Quine_(computing)) 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.)

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 27, 2024, 1:58pm UTC](https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639/3 "2024-03-27T13:58:05Z")

</div>

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.

```python
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)

```

---

<div class="post-metadata">

**Author:** ![hansgeunsmeyer](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hansgeunsmeyer/32/15188_2.png) [@hansgeunsmeyer](https://discuss.python.org/u/hansgeunsmeyer)\
**Post date:** [March 27, 2024, 2:10pm UTC](https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639/4 "2024-03-27T14:10:13Z")

</div>

> [@JamesParrott](#):
>
> 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.

Very true 🙂

---

<div class="post-metadata">

**Author:** ![Resuscitating](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/resuscitating/32/8782_2.png) [@Resuscitating](https://discuss.python.org/u/Resuscitating)\
**Post date:** [March 28, 2024, 2:08pm UTC](https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639/5 "2024-03-28T14:08:33Z")

</div>

> [@hansgeunsmeyer](#):
>
> (Without using `open` or any io functions.)

> [@JamesParrott](#):
>
> ```py
> 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`.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 28, 2024, 3:13pm UTC](https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639/6 "2024-03-28T15:13:07Z")

</div>

Drat. You’re right.

> <https://github.com/python/cpython/blob/c1712ef066321c01bf09cba3f22fc474b5b8dfa7/Lib/linecache.py#L136>

---

<div class="post-metadata">

**Author:** ![Resuscitating](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/resuscitating/32/8782_2.png) [@Resuscitating](https://discuss.python.org/u/Resuscitating)\
**Post date:** [March 28, 2024, 8:45pm UTC](https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639/7 "2024-03-28T20:45:38Z")

</div>

If using `.pth` files is allowed, one could install a codec that prints the  
source code when it receives it for decoding.

```py
# -*- coding: quine -*-

```

… but that feels like cheating, since it requires external code.

# 

A fun (and not cheating) quine is this:

```py
  File "<path to file goes here>", line 1
    File "<path to file goes here>", line 1
IndentationError: unexpected indent

```

When run, it outputs:

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

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [March 28, 2024, 11:06pm UTC](https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639/8 "2024-03-28T23:06:17Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![Resuscitating](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/resuscitating/32/8782_2.png) [@Resuscitating](https://discuss.python.org/u/Resuscitating)\
**Post date:** [April 11, 2024, 6:18am UTC](https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639/9 "2024-04-11T06:18:53Z")

</div>

> [@JamesParrott](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [April 11, 2024, 6:34am UTC](https://discuss.python.org/t/lets-type-type-type-yes-its-a-valid-python-code/49639/10 "2024-04-11T06:34:16Z")

</div>

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

```python

```

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