Grilled Cheese

ExploreLog inSign up
Terms of UsePrivacy PolicyCommunity StandardsHelpGet the app

Grilled Cheese is a product of Village Compute

Version devBuilt at: 2026-10-12 04:31:30 EDT

Explore

PostsPeople
LatestRanked
@trey.ioOct 11, 2026, 2:48 PM

Python Tip #284 (of 365):

Import from another directory by adding it to sys.path. 🧡

sys.path.insert(0, str(Path(__file__).parent / "vendored_utils"))

This is a bit of a hack. It works great in your own project, but I wouldn't want to see it in an installable package.

#Python #DailyPythonTip

@trey.ioOct 10, 2026, 2:48 PM

Python Tip #283 (of 365):

Delay slow imports until they're needed. 🧡

#Python #DailyPythonTip

@trey.ioOct 9, 2026, 1:27 PM

Python Tip #282 (of 365):

Read your package's data files with importlib.resources. 🧡

>>> from importlib import resources
>>> resources.files("dinner").joinpath("recipes.json").read_text()
'{"tacos": ["tortillas", "beans", "salsa"]}\n'

#Python #DailyPythonTip

@trey.ioOct 8, 2026, 5:27 PM

Python Tip #281 (of 365):

Bundle your package into one runnable file with zipapp. 🧡

$ python3 -m zipapp src -m "greet.cli:main" -p "/usr/bin/env python3" -o greet.pyz
$ python3 greet.pyz
Hello from greet!
$ ./greet.pyz
Hello from greet!

#Python #DailyPythonTip

@trey.ioOct 7, 2026, 10:47 PM

Python Tip #280 (of 365):

Make your package runnable with a __main__‍.py file. 🧡

greet
β”œβ”€β”€ cli‍.py
β”œβ”€β”€ __init__‍.py
└── __main__‍.py

#Python #DailyPythonTip

@trey.ioOct 6, 2026, 3:02 PM

Python Tip #279 (of 365):

Simplify your package's interface with __init__‍.py. 🧡

Yesterday's tip noted that an empty __init__‍.py file is common and completely fine.

But that file doesn't HAVE to be empty.

#Python #DailyPythonTip

@trey.ioOct 5, 2026, 10:47 PM

Python Tip #278 (of 365):

Always include a __init__‍.py file in your Python packages. Even an empty one. 🧡

food_utils
β”œβ”€β”€ fruits‍.py
β”œβ”€β”€ __init__‍.py
└── non_fruits‍.py

A package is a directory that Python treats like a module.

#Python #DailyPythonTip

@trey.ioOct 5, 2026, 2:46 AM

Python Tip #277 (of 365):

For substring checks, use the "in" operator. 🧡

Python has a special operator that's meant JUST for checking whether one thing is contained inside another:

>>> part = "Python"
>>> whole = "I enjoy writing Python code."
>>> part in whole
True

#Python #DailyPythonTip

@trey.ioOct 3, 2026, 11:47 PM

Python Tip #276 (of 365):

Make self positional-only when accepting "**kwargs". 🧡

This class accepts any keyword argument... except self:

class Namespace:
def __init__(self, **kwargs):
for name, value in kwargs.items():
setattr(self, name, value)

#Python #DailyPythonTip

@trey.ioOct 2, 2026, 10:01 PM

Python Tip #275 (of 365):

Pre-fill arguments with functools.partial. 🧡

>>> from functools import partial
>>> hex_to_int = partial(int, base=16)
>>> hex_to_int("CAFE")
51966

This is sometimes called "partially evaluating" a function.

#Python #DailyPythonTip

@trey.ioOct 1, 2026, 10:01 PM

Python Tip #274 (of 365):

Prefer to name your functions. 🧡

Instead of this:
sorted(items, key=lambda item: int(item.split()[0]))

#Python #DailyPythonTip

@trey.ioSep 29, 2026, 11:35 PM

Python Tip #272 (of 365):

Do something or return something, but not both. 🧡

The list sort method returns None:

>>> numbers = [4, 2, 7, 1, 5]
>>> x = numbers.sort()
>>> print(x)
None
>>> numbers
[1, 2, 4, 5, 7]

#Python #DailyPythonTip

@trey.ioSep 29, 2026, 11:20 PM

Python Tip #273 (of 365):

Avoid needless wrapper functions. 🧡

This lambda does nothing but call len:

sorted(words, key=lambda s: len(s))

So pass len itself:

sorted(words, key=len)

#Python #DailyPythonTip

@trey.ioSep 29, 2026, 12:35 AM

Python Tip #271 (of 365):

Return, don't print. 🧡

If your function only seems to "work" in the REPL, it may be printing when it should be RETURNING.

#Python #DailyPythonTip

@trey.ioSep 27, 2026, 11:00 PM

Python Tip #270 (of 365):

Move and copy files with pathlib. 🧡

In Python 3.14, Path objects gained move and copy methods, and copy works on whole directories:

>>> from pathlib import Path
>>> taxes = Path("taxes")
>>> taxes.copy("taxes-backup")
PosixPath('taxes-backup')

#Python #DailyPythonTip

@trey.ioSep 26, 2026, 10:52 PM

Python Tip #269 (of 365):

Walk directory trees with Path.walk. 🧡

The walk method (Python 3.12+) is the pathlib version of os.walk. For each directory in the tree, you get the directory's path, a list of its subdirectory names, and a list of its file names.

#Python #DailyPythonTip

@trey.ioSep 26, 2026, 4:30 AM

Python Tip #268 (of 365):

Show relative paths with relative_to. 🧡

Absolute paths are noisy:

>>> print(path)
/home/trey/taxes/archived/2017/invoices
>>> taxes
PosixPath('/home/trey/taxes')
>>> print(path.relative_to(taxes))
archived/2017/invoices

#Python #DailyPythonTip

@trey.ioSep 24, 2026, 5:02 PM

Python Tip #267 (of 365):

Change file extensions with with_suffix. 🧡

A ".rst" file path that needs to turn into a ".md" file path:

>>> from pathlib import Path
>>> old_path = Path("uvrs/readme.rst")
>>> old_path.with_suffix(".md")
PosixPath('uvrs/readme.md')

#Python #DailyPythonTip

@trey.ioSep 23, 2026, 5:02 PM

Python Tip #266 (of 365):

Reread a file with seek(0). 🧡

Try to .read() a file twice, and you'll get nothing the second time:

>>> f = open("my_file.txt")
>>> f‍.read()
'This is my file πŸ‘‹\nThis is line 2\n'
>>> f‍.read()
''

#Python #DailyPythonTip

@trey.ioSep 22, 2026, 5:02 PM

Python Tip #265 (of 365):

Use next() to get the first line in a file. 🧡

#Python #DailyPythonTip

Load more