5 Cool Hacky Python Features That Hardly Get Used In Production
Not all features get their airtime in production Python code.
Code is read more than it is written
As such, in production code, stuff that likely hurts readability or causes confusion to the reader are often rejected. Here are 5 cool features that are unfortunately pretty rare in production code.
1) *args and **kwargs
*args
allows us to pass any number of positional arguments into a function.
**kwargs
allows us to pass any number of keyword arguments into a function.
But unfortunately, readability is impacted when we use too much of *args
and **kwargs
:
we might not know exactly what goes into
*args
and**kwargs
it’s difficult to add type hints to
*args
and**kwargs
production code has a preference for explicit keyword arguments as this tells the reader explicitly the stuff that is passed into the function.
Cases where *args
and **kwargs
are more likely to appear in production code:
In decorators
In library code used by other developers
But otherwise, not much
2) The walrus operator :=
The walrus operator (:=) does 2 things:
it assigns a variable to a value
it returns the value in the expression
Here’s a dumb example:
Here’s how we can rewrite it using the walrus operator (:=)
While the walrus operator saves us lines of code in a cool way, it hurts readability. Code readers often have to spend extra few seconds figuring out how the walrus operator comes into play.
As such, I’ve noticed that code reviewers often leave a comment telling us to remove the walrus operator, and just use =
normally.
3) `x = yield something`
The x = yield something
expressions allows us to send input to our generator function.
^ here, we use gen.send(100)
to send the value 100
into our generator object.
While this is cool, I’ve not seen this in production code:
there’s no strong use case for it
it looks unintuitive and hurts readability a lot
4) name mangling and @property
This surprised me quite a bit at the start.
In Python OOP class, we were often taught:
to use “private” variables (that are not actually private)
and to use the
@property
decorator to control read/write access to the attribute
name
is a readonly attribute as it only has a getter methodage
is a read/write attribute as it has both a getter and setter method
In production code, nobody seems to care. They write classes like this:
Suspected reasons:
@property
doesn’t actually add much value to the code, so there’s no reason to use it@property
needlessly makes the code longer@property
makes the code less readable, as we have to now deal with the getter and setter methodsIt’s annoying to write
@property
5) metaclasses
A metaclass is a class that creates another class when initialized. Theoretically, we use it to customize the behaviour of other normal classes.
Here’s a sample metaclass that adds a custom hello
method to classes.
Let’s use it:
^ notice that classes that use our metaclass metaclass=HelloMetaclass
will automatically have a hello
method added.
But metaclasses are almost non-existent in production code. Probably because it’s easier and more readable to just use inheritance. Or to just define the function elsewhere.
Conclusion
Hopefully this was clear and easy to understand
If You Wish To Support Me As A Writer
Buy my book — 101 Things I Never Knew About Python at https://payhip.com/b/vywcf
Clap 50 times for this story
Leave a comment telling me your thoughts
Highlight your favourite part of the story
Thank you! These tiny actions go a long way, and I really appreciate it!
LinkedIn: https://www.linkedin.com/company/the-python-rabbithole/