# Filename and line number in f-string debugging output

**URL:** <https://discuss.python.org/t/filename-and-line-number-in-f-string-debugging-output/1650>\
**Category:** Ideas\
**Created:** [May 10, 2019, 6:44am UTC](https://discuss.python.org/t/filename-and-line-number-in-f-string-debugging-output/1650 "2019-05-10T06:44:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![xtreak](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/xtreak/32/267_2.png) [@xtreak](https://discuss.python.org/u/xtreak)\
**Post date:** [May 10, 2019, 6:44am UTC](https://discuss.python.org/t/filename-and-line-number-in-f-string-debugging-output/1650/1 "2019-05-10T06:44:03Z")

</div>

I have been playing around with f-string debugging feature. There are some cases where I print a certain variable in different parts of a program and I thought adding filename:lineno would help. This was mostly inspired by Rust dbg! macro : [https://doc.rust-lang.org/beta/std/macro.dbg.html](https://doc.rust-lang.org/beta/std/macro.dbg.html)

```auto
dbg!(a * 2) // [src/main.rs:2] a * 2 = 4

```

```python
# prog.py
name = "karthikeyan"
age = 25

print(f"{name = }")
print(f"{age = }")

age += 1

print(f"{age = }")
print(f"{name = :-^20}")
print(f"{str.capitalize(name) = :-^20}")

```

```auto
➜ cpython git:(master) ✗ ./python.exe prog.py
name = 'karthikeyan'
age = 25
age = 26
name = ----karthikeyan-----
str.capitalize(name) = ----Karthikeyan-----

```

Adding filename and lineno support in my branch by prepending it to `expr_text` in Python/ast.c .

```auto
➜ cpython git:(fstring-filename) ✗ ./python.exe prog.py
[prog.py:4] name = 'karthikeyan'
[prog.py:5] age = 25
[prog.py:9] age = 26
[prog.py:10] name = ----karthikeyan-----
[prog.py:11] str.capitalize(name) = ----Karthikeyan-----

```

At this point using a logger would be recommended. The issue and python-ideas discussion was focused more on keeping it as simple as possible with just repr but having it in built since it’s for debugging looks nice to me. I am okay with it being rejected but personally I think it’s nice for debugging.

@ericvsmith @larry Thoughts?

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [May 10, 2019, 8:12am UTC](https://discuss.python.org/t/filename-and-line-number-in-f-string-debugging-output/1650/2 "2019-05-10T08:12:40Z")

</div>

-1 on this being included by default. I have plenty of use cases that wouldn’t be described as debugging (in fact, they are probably 99-100% of my use cases!) and for those cases, adding the location data would be wrong.

I don’t mind if it’s added as an opt-in feature that people have to request to get it - but personally I doubt I’d ever use it, so I’m fine with not having it.

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [May 10, 2019, 9:48am UTC](https://discuss.python.org/t/filename-and-line-number-in-f-string-debugging-output/1650/3 "2019-05-10T09:48:41Z")

</div>

> [@pf\_moore](#):
>
> I don’t mind if it’s added as an opt-in feature that people have to request to get it

Like… `import logging`?

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [May 10, 2019, 10:11am UTC](https://discuss.python.org/t/filename-and-line-number-in-f-string-debugging-output/1650/4 "2019-05-10T10:11:30Z")

</div>

Precisely.

```python
import logging

l = logging.Logger("")
h = logging.StreamHandler()
f = logging.Formatter(fmt="[{filename}:{lineno}] {msg}", style="{")
h.setFormatter(f)
l.addHandler(h)
l.info("Hello")

```

Output:

```auto
>py .\t.py
[t.py:8] Hello

```

(Nobody ever said the logging module was terse, but all of that could be put into a utility module).

---

<div class="post-metadata">

**Author:** ![ericvsmith](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@ericvsmith](https://discuss.python.org/u/ericvsmith)\
**Post date:** [May 10, 2019, 2:42pm UTC](https://discuss.python.org/t/filename-and-line-number-in-f-string-debugging-output/1650/5 "2019-05-10T14:42:26Z")

</div>

I think the file name and line number are too much magic (says the guy who just added “=“!).

If I were really going to do this (which I am definitely not suggesting), I would add something that just expands to the file name, and something else that just expands to the line number. To add it to “=“, even optionally, seems like you’re conflating things.

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [May 10, 2019, 3:46pm UTC](https://discuss.python.org/t/filename-and-line-number-in-f-string-debugging-output/1650/6 "2019-05-10T15:46:17Z")

</div>

This may be implemented as combination of f-string debugging and special function which prints the filename and line number of the caller (using `sys._getframe()`).

```python
dbg(f"{name = }")

```

We now have the `breakpoint()` builtin, so may add also a builting for debugging print.

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [May 10, 2019, 3:49pm UTC](https://discuss.python.org/t/filename-and-line-number-in-f-string-debugging-output/1650/7 "2019-05-10T15:49:35Z")

</div>

Other option – use the `trace` module with the tracer which prints the filename and the line number for lines containing `print`, `f"{...=}"` or special comment.

---

<div class="post-metadata">

**Author:** ![xtreak](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/xtreak/32/267_2.png) [@xtreak](https://discuss.python.org/u/xtreak)\
**Post date:** [May 10, 2019, 4:15pm UTC](https://discuss.python.org/t/filename-and-line-number-in-f-string-debugging-output/1650/8 "2019-05-10T16:15:53Z")

</div>

This looks like a better idea than adding it to =. I must admit my initial proposal was just to emulate dbg! macro with f-string but as per the arguments I think there are broader use cases with f-string syntax where filename and line number could be a noise.

Having it as a separate function seems good where users can explicitly opt in for it instead of print() to use it for debugging. There are pypi packages like icecream [0] that implement this type of utility function and could be a good one to have in core.

[0] [https://github.com/gruns/icecream](https://github.com/gruns/icecream)

---

<div class="post-metadata">

**Author:** ![aldwinaldwin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aldwinaldwin/32/1075_2.png) [@aldwinaldwin](https://discuss.python.org/u/aldwinaldwin)\
**Post date:** [June 4, 2019, 9:58am UTC](https://discuss.python.org/t/filename-and-line-number-in-f-string-debugging-output/1650/9 "2019-06-04T09:58:30Z")

</div>

+1 … I find the idea more convenient for debugging than import logging because l.info("") will always go to my logfiles. Not as default for sure, but with a dedicated special character like “=[” or “=^” or “=#” (cf. =!s =!a …).

`print(f'name = [')`

It resolves my stupid habit of having `print('hereiam123:'+str(xyz))`.
