DEV Community

Discussion on: 1-minute guide to real constants in Python

Collapse
 
rhymes profile image
rhymes • Edited

I don't know how I feel about all of this. I see the value in helping tools with type declarations and mypy seems cool especially for large code bases. I'm also going to see if I can adopt optional static typing in a future project.

I'm not sure about trying to turn Python into a "closed" language is the answer though. Final classes in Java are one of ugliest thing they have, because if the API you create has an error (which can happen) all you're doing with this is make the developer's life harder (or in the case of Python just telling them to bypass mypy I guess).

One of the tenents of Python is its freedom and conventions. I can definitely tell you that in all these years the times I had to hack badly written "contracts" or APIs are more than you think. What if I couldn't because the library developer didn't trust his fellow developer enough to leave them the ability to "upgrade" the quality of the provided code? Even trust them to make a mess. If the code we wrote was perfect I would probably understand the need for final classes, but it's not.

Going over your conclusions:

it is clear from the definition what is a constant or a concrete realization and what is not

for classes, you can use use the module abc to declare if a class or a method are abstract

our users will have strict API boundaries that can not be violated

Don't get this the wrong way but... who cares :-D

What I'm trying to say is that you can't anticipate avoiding any mistakes in API design and the web is full of people asking "why this Java method is private, please make it public because we need it" or "I just had to patch your Python class because this or that, fortunately I can do it, here's the diff to fix the problem".

we can build closed systems that are not tolerant of breaking the rules

"practicality beats purity", that's one of the lines in the Zen of Python.

If it works for you fine, but I'm not sure that these last two points are "pros" :-)

I've always had the feeling that Python has a silent contract with the developers saying: I trust you, you're an adult, you have the power to mess up, use it wisely.

it enforces composition over inheritance, which is a well-known best practice

this is always a good thing :)

Collapse
 
sobolevn profile image
Nikita Sobolev

Your points are perfectly valid. And I absolutely agree with you.

The thing I love about python is that you can still do whatever you want!

Just add # type: ignore inline comment and your mypy issue will be gone.
That's why we can have best of both worlds: closed systems that guide you and enough freedom to be an adult.

Collapse
 
rhymes profile image
rhymes

Thanks Nikita!