Bob Belderbos | @bbelderbos@fosstodon.org Profile picture
Software developer passionate about building useful tools and helping people grow their #Python and #programming skills through @Pybites
Feb 2 6 tweets 1 min read
Stop scattering `if/elif` checks across your code to manage workflow states.

If your feature has a real lifecycle (draft → review → approved → published), model it as a **state machine**. Image With `transitions`, you attach the machine to your existing object (`model=content`) and it injects the trigger methods + keeps a `state` field for you.
Jan 29 4 tweets 1 min read
"The borrow checker is fighting me!" 🥊

No, it's not. It's saving you from a 3 AM debugging session three months from now.

In Python, variables are references. You pass an object around, everyone shares it.

In Rust, by default, Ownership is exclusive. Image Why? Because if `s1` and `s2` both "owned" the memory, they would both try to free it when they go out of scope. -> Double Free Error. -> Security Vulnerability.
Jan 23 7 tweets 2 min read
You don’t learn by watching. You learn by breaking things. 💡

We've spoken with developers who’d watched multiple 10+ hour Udemy courses.

They knew the theory: Django, Docker, AsyncIO. Image But when I asked them to:
• spin up a simple CLI tool from scratch, or
• deploy a small app to the cloud…

They froze.

“I usually just follow the video.”

That’s tutorial hell.

It feels productive, but you’re borrowing someone else’s problem-solving.
Aug 24, 2023 13 tweets 2 min read
🚀 10 useful things you can do more easily with pathlib than os in #Python ...

Check out this thread to see how to write more readable, simple and beautiful code with `pathlib` ... 1️⃣ Creating/Joining Paths:

os:
p = os.path.join('folder', 'file.txt')

pathlib:
p = Path('folder') / "file.txt"
Aug 24, 2023 5 tweets 1 min read
Customizing Class Creation with __init_subclass__ 💡

In #Python, the __init_subclass__() method is a powerful and lesser-known tool for customizing class creation behavior in subclasses.

Example: class Plugin:     plugins = []      def __init_subclass__(cls, **kwargs):         super().__init_subclass__(**kwargs)         cls.plugins.append(cls)  class ConcretePlugin(Plugin):     pass  class AnotherPlugin(Plugin):     pass  print(Plugin.plugins)   # Output: [<class '__main__.ConcretePlugin'>, <class '__main__.AnotherPlugin'>]   In the provided code:

📘 Plugin is a base class.

🚀 Whenever a subclass of Plugin is defined, __init_subclass__ of Plugin is automatically called.

🔍 Inside the __init_subclass__ method, the newly defined subclass (cls in this context) is appended to the plugins list.
Apr 14, 2022 22 tweets 6 min read
20 cool things you can do with #Python's built-in functions 🐍🧵 1. Make a dictionary from two lists:
Apr 13, 2022 4 tweets 1 min read
When you build up a #Python string use a list over string concatenation (+=).

See stackoverflow.com/a/3055541:
> Strings are immutable and can't be changed in place. To alter one, a new representation needs to be created.

So that happens repeatedly here = slower. Btw instead of loop + append, I could also have used a list comprehension inside the .join()
Dec 1, 2021 14 tweets 2 min read
Don't get stuck in tutorial paralysis, the best way to learn #Python and software development is to build real world applications 🧵 You will hit so many issues, you'll have to constantly look things up, Stack Overflow becomes your new best friend and that is ok. It will turn you into a good developer.
Aug 29, 2020 5 tweets 2 min read
5 tips that will improve the design and quality of your #software:

1. Write shorter functions (methods) that do just one thing ("single responsibility principle").

2. Decrease the number of decision points (aka "cyclomatic complexity") per unit (function, method, etc).
... ...
3. Avoid duplication at all cost!

4. Keep your interfaces small (e.g. the number of arguments a function or class receives).

5. Separate concerns by making your code more modular.