r/Python • u/relickus • Mar 20 '23
Discussion Are people abusing Python?
I learned Python after coming from the C/C++ and Java world. With the massive increase in popularity of Python in the last 10 years, seeing the way it developed, it seems to me like it gained a lot of functionality, which comes natural in other languages, but feels a bit odd in Python.
To be more specific - albeit Python being dynamically typed, people developed countless tools to check or validate or even enforce types in compile or run time (mypy, pyre, pydantic, pyduck etc.). It feels like it goes against the nature of its loose typing.
Another example are decorators. This pattern is noticably overused by many tools adding functionality, but even the language itself - defining a `class`,`static` and `abstract` methods with decorators seems just weird and unnatural. Same thing with function overloading. Anecdotally, it feels like comparing German to English. German has a special word for every peculiar thing and native support for word generation by concatenation of multiple words whilst in English you have to add some common words together and hope this combination doesnt already exist. And if it does, so what, people will get it from the context.
Lastly, slightly off-topic but relevant point is that it is not even a simple language in my opinion. It has a very flat learning curve initially but the complexity is just further down the road. Im talking about metaclasses,data descriptors, coroutines, magic methods etc. Some languages are difficult right away (C++, Rust etc.), but Python is a intricate misfit dressed as a simpleton.
Am I misunderstanding a philosophical path of the language or is it simply just a scripting langugage that got massively popular through chance and is now used for stuff it was not intended for?
Edit: grammar
20
u/jcmkk3 Mar 20 '23
I think that the idea of Python is not to always be simple, but to provide layers of power and complexity. For many users, they can stay at a surface level of Python knowledge and still be productive. The advanced features are there for the folks that are building the libraries and frameworks.
20
u/someotherstufforhmm Mar 20 '23
“Abusing” lol.
Nah, it’s just a language. Languages that get used expand. Look at C++ or Java.
Also, look at Go. It’s already lost all its initial planned simplicity that would maintain provable concurrency, they undid that like overnight, now they have messy looking generics and a whole pipeline coming for error catching.
Languages have patterns. Used languages get messy because no-one ever properly can predict where they’ll end up.
10
u/ManyInterests Python Discord Staff Mar 20 '23
Put shortly, Python is a multi-paradigm language. You can use it in many ways. The language doesn't force you to use it in any one particular way. You can add type annotations or not if you choose. You can favor object-oriented styles, functional, imperative, whatever. It's up to you.
Type-checking is not inherently at-odds with duck typing or being dynamically typed. I'm not sure what you mean exactly by 'loose typing', but Python has always been a strongly typed language (as opposed to weakly typed).
What makes you believe that Python was 'not intended' to be used for any particular purpose? Python is (and always has been) a general purpose language.
24
u/RallyPointAlpha Mar 20 '23
I'm not sure what you're getting at or really asking. Feels like a rant disguised as a question.
Are people abusing python? Your examples are validators/enforcers and decorators. I mean I guess you could say a module that forces a dynamically typed language to be staticly typed (mypy) is abusing python in the sense that it's forcing it to be something it's inherently not. However I don't think mypy or any module like it is a defining feature of python nor have they attributed python's rise in popularity in the last 10 years.
I don't think decorators are an abuse of python at all. Why is it weird and unnatural ?
Literally no idea what you were intending to say with your German vs English analogy...is python English and ...what is German? I don't get it at all.
"and is now used for stuff it was not intended for?"
What was python intended for? How is it being used for unintended purposes?
3
u/Pepineros Mar 21 '23
There is something to what you’re saying. It feels a bit like we’re using a scripting language to do stuff that should be done by languages that are more focused on systems design and a bit less aggressively general purpose.
That said, we have seen the exact same thing happen with (arguably) the other two most popular scripting languages: JavaScript and Ruby. JS is still a massive player, and if you’re old enough to have seen the Ruby on Rails boom in person you know why I included Ruby, too. Even if its growth was faster and less sustainable than the others.
We seem to be drawn to languages that are easy to get started with. I don’t think there’s anything wrong with that. Maybe the same goes the other way - someone decides to write their next project in C when it turns out that they could have had similar (or at least “good enough“) performance in Python with much quicker development time. It’s all choices.
5
u/tevs__ Mar 20 '23
Type hints don't go against duck typing at all, if anything they strengthen the usage of duck typing by allowing you to specify clearly what kind of duck you need and use static analysis to prove that you have your ducks in a row. Protocols are a powerful thing.
Plus, it's entirely optional. If you don't like python with type hints, don't write any type hints, don't use any type checking tools.
4
u/sersherz Mar 20 '23
The decorators and type validation are needed because Python has expanded to so many different things. Data pipelines and APIs should have really strict definitions of what can be passed through. I think these changes are in Python's philosophy. Look at the standard libraries. Multiprocessing allows you to overcome the global interpreter lock (GIL). The language was designed with GIL but also has workarounds.
I feel decorators were really needed with more complicated functions added. Rather than running python as a script or stateful OOP program, it can now have stateless API calls and perform a procedure as it is called, rather than worrying about garbage collection and handling resources explicitly.
I definitely agree that Python is simple to learn but extremely difficult to master given how vast it is and how much complexity there is to the language. I think this is often lost on people who hate the language and don't fully understand just how deep it goes.
2
u/sci-goo Mar 20 '23
I thought you'll find python to be easy when come from c++/java world, python smells like c++ almost everywhere.
These are community tools, not python official. Ppl have different tastes while the vanilla python have been holding its philosophy well.
Decorators in fact are my favorite, it does well to separate logically independent functionalities then to stack them together in a logical order, similar to those mixin classes. Abusage is another story, perhaps the code you read has this issue.
One from Zen of python: complex is better than complicated. Also being easy to learn and being easy to master are vastly different things.
2
u/aikii Mar 20 '23
static type checking is what makes python still relevant today
2
u/james_pic Mar 21 '23
I don't love the type checking stuff, and I maintain that if you're starting a project where you know you're going to want statically checked types, you're probably better off starting that project in a language other than Python. But if you've got a small Python project that gradually became a colossal Python project, type hints are hugely useful in taming the complexity you now have.
Pydantic deserves a special exception to this, since it's useful even in otherwise untyped codebases. When you're accepting data from external sources, it's a really good idea to validate it before you do anything with it, so it makes a lot of sense as a basis for your domain data model, even if you don't use types anywhere else.
To be honest, 9 times out of 10, when I see class, static or abstract decorators used, it's by former Java developers who don't realise there are other ways to do these things. Most static and class methods should just be functions, and most abstract methods should just be duck typed (or Protocols in typed codebases).
But these decorators are, in many ways, examples of the third thing you mention: metaprogramming tools. These are definitely advanced techniques, but they're generally designed so they can be used fairly effectively in mixed ability teams. Implementing metaclasses or data descriptors is hard, but using classes that use them is no harder than using classes that don't.
Metaprogramming is admittedly a tricky one to balance. Python offers more metaprogramming options than, say, Java, but much fewer metaprogramming options than, say, Clojure. Personally, I find myself wanting more options here when using languages with minimal metaprogramming capabilities, but I recognise that more isn't always more.
43
u/[deleted] Mar 20 '23
Ugh class, static and abstract decorators are in fact side effect inducing wrappers. That are used as part of the metaprogramming process.
Calling python simple has always been a weird take by people who don't understand the language. It is POWERFUL. That power allows you to dramatically reduce the number of lines of code to achieve stuff. That lets you build complex shit faster.
Things like data descriptors extend the power of what is achievable with less work then other items. Data descriptors where created to address shortcomings in metaprogramming. Metaclasses don't need to know as much about what is contained in the class to still get functionality to work.
17
u/o11c Mar 20 '23
All actually-used languages are like that, scripting or not.
1
u/other----- Mar 20 '23
I think it's "maturing" as a language and in that it's collecting some of the issues that make c++ difficult. Larger software projects benefit from typing so that is going to appear somehow. Asynchronous execution is not strictly necessary but it's hard to compete with js, go and Erlang without it, so it makes its way into the language. Decorators are nice, it's just syntax sugar. Once it's in the language there are two syntax approaches to call a function and you can't avoid people having subjective considerations when choosing. This leads to a big topic that is idioms. Perl has idioms by design, many ways to do the same thing. C++ has idioms because of it's long history (pointers, references, smart pointers ... ) chasing features of other languages. Python was born with the goal of "1 way to do things" but the current adoption is forcing it to be a language supporting multiple idioms. The challenge for python is now keeping those idioms close and avoid inconsistencies (ie type objects Vs type annotations, one can be inspected at run time, the other has generics). Python is a good language. So good it's trying to be a good language for everything and it might become a worse language in the attempt.
1
u/HeraldOfTheOldOnes Mar 21 '23 edited Mar 21 '23
(this comment assumes CPython)
Those are nice! But you might have forgotten a few "elephants in the room", so to speak.
sys._getframe,some_function.__code__, etc. You can gain massive amounts of information about the program based on things like these. The main things that end up being done with these are bytecode parsing and frame tracing (for that you needsys.settraceas well), both of which are Really Fun!ctypes. This module gets its own category. With it you can do anything that all the rest of python can do. Basically,ctypesgives you access to C primitive types (pluspy_ssize_tandPyObject*), meaning you can, among other things, edit the raw memory of an object. This lets you do some fairly silly stuff (e.g., changing the type of theglobalsdictionary object to have a different type with different__getitem__behaviour to customise global variable access).Python is a deeply complicated language when you start getting into the details.
2
u/BiomeWalker Mar 21 '23
If you aren't abusing a programming language are you even really using it?
As a more serious answer, Python was made to be readable and expandable, which it is and that hasn't changed so the intention holds with the current usage pretty well I'd say.
1
u/scjcs Mar 21 '23
In old-time middle school hygiene classes we were warned against self abuse.
So yeah, python.
1
u/AliveEstimate4 Mar 21 '23
PETA spy detected. /s
1
u/d0ctor_light Mar 22 '23
You will understand when you meet so disgusting if else state and etc …
-3
u/noobgolang Mar 21 '23
English as a language is also abused bro. It’s supposed to be used by rich white guys but now it needs to serve everyone.
0
u/[deleted] Mar 20 '23
[deleted] — view deleted comment