MyClass._dangerous(self) is a private method.
We could have implemented the same functionality without a private
method as follows:
- Define a class
InnerClasswith the same
InnerClass.dangerous(self)with the same logic of
MyClassinto a wrapper class over
InnerClass, where the wrapped attribute is private.
- Proxy all necessary work into
This might seem onerous,
but consider that now,
dangerous is part of the public
interface of a class,
and would need to be explicitly documented as to assumptions and guarantees.
This documentation would have had to have been in comments around
_dangerous anyway --
in order to clarify what its assumptions are,
and what invariants it is violating in
otherwise, maintaining the code that calls
would be hard.
this documentation is almost certain to be missing.
The existence of
implies this was an almost mechanical refactoring
of similar code into a method,
with the excuse of "its private"
used to avoid considering the invariants and interface.
Even if the documentation did exist,
now it is possible to unit-test that the documentation is correct.
if we use best practices when we define
in other words,
avoid creating an
InnerClass object in the initializer,
and only creating it in an
we are also in a good position to unit test
presented the worst case:
the code for
_dangerous touches absolutely every data member
In real life,
the worst case is not often encountered.
When we look at a private method as a code smell,
and contemplate the best way to refactor it away,
it turns out that we often can find a coherent set of attributes
that really does make sense as
InnerClass on their own merits.
Credit: This is based on an off-handed comment Glyph made in his blog post about attrs. I am also grateful to him for reviewing a draft copy of this post, and making many useful suggestions. All mistakes in interpretation or explanation are mine alone.
PyCon US 2018 Twisted Birds of Feather Open Space Summary
PyCon 2018 US Docker Birds of Feather Open Space Summary
We started out the conversation with talking about writing good Dockerfiles. There is no list of "best practices" yet. Hynek reiterated for us "ship applications, not build environments". Moshe summarized it as "don't put gcc in the deployed image."
We discussed a little bit what we are trying to achieve …read more
Announcment: My book, from python import better, has been published. This post is based on one of the chapters from it.
When Python started out, one of the oft-touted benefits was "batteries included!". Gone were the days of searching for which XML parsing library was the best -- just use the …read more
Web Development for the 21st Century
(Thanks to Glyph Lefkowitz for some of the inspiration for this port, and to Mahmoud Hashemi for helpful comments and suggestions. All mistakes and issues that remain are mine alone.)
The Python REPL has always been touted as one of Python's greatest strengths. With Jupyter, Jupyter Lab in its latest …read more
(Thanks to Paul Ganssle for his suggestions and improvements. All mistakes that remain are mine.)
When exposing a Python program as a command-line application,
there are several ways to get the Python code to run.
The oldest way,
and the one people usually learn in tutorials,
is to run
Random Bites of Pi(e)
In today's edition of Pi day post, we will imagine we have a pie. (If you lack imagination, go out and get a pie.) (Even if you do not lack imagination, go out and get a pie.)
As is traditional, we got a round pie. Since pies are important, we …read more
The Python Toolbox
I have written before about Python tooling. However, as all software, things have changed -- and I wanted to write a new post, with my current understanding of best practices.
As of now,
pytest has achieved official victory.
Unless there are overwhelming reasons to use something else,
strongly consider using …
Jupyter for SRE
Jupyter is a tool that came out of the data science community. In science, being able to replicate experiments is of the utmost importance -- so a tool where you can "show your work" is helpful. However, being able to show your work -- have colleagues validate what you have done, repeat …read more