The 1 Line of Code Every Python Developer Should Know

Опубликовано: 28 Январь 2022
на канале: Zack Plauché
69
5

tl;dr: print(*dir(my_obj), sep='\n')

Speed up your Python learning with this useful Python one liner 🙂.

Learn how to quickly explore Python objects / modules / datatypes / etc. with this useful 1 liner to get a feel for how an object / package works and what it's capable of.

I came up with this (I mean, likely someone else did too) while reading the Fluent Python book where he was talking about unpacking using the * command.

It works like this:
If you have a list of items, like

my_list = [1, 2, 3]

You could print the whole list like

print(my_list)

Or you could add each item as a separate arg like this:

print(my_list[0], my_list[1], my_list[2])

Or, even shorter:

print(*mylist)

It's the same functionality that lets you assign different variables, like:

a, b, c = [1, 2, 3]

Pretty useful, neat, and good for readability 🙂.

---
Want me to help teach you code? You can reach out to me via Codementor.io and I'd love to help you 🙂.

Here's my codementor profile: https://www.codementor.io/@zackknowsp...

---
My Links:
My Website: https://www.zackplauche.com/
Codementor: https://www.codementor.io/@zackknowsp...
Wyzant: https://www.wyzant.com/Tutors/LearnPy...
GitHub: https://github.com/ZackPlauche
Stack Overflow: https://stackoverflow.com/users/10415...