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
Leonieke Profile picture 1 subscribed
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.