This is a question for people more experienced with Python, but everybody feel free to answer if you feel like you can provide something decent to the discussion.
Also feel free to explain why you feel that way and your experiences with Python and the paradigms.
Used it a ton in the art departments of vfx and game dev. Im talking about the tools that make assets, not the game engine or a runtime scripting language. More like the stuff launching and running in Maya, or Houdini, or Substance, etc.
Most of this is already highly OO, and there’s a lot of interaction with C++. Python is the perfect language for this. There’s a lot of rapid interation and gluing many different services and data together. Also you’re waiting on file IO or some massive scene graph update all the time so having the tools be slightly slower doesn’t matter. Also, at least in vfx, there’s mixed Linux/Windows/Mac and it’s great for that. ALSO art teams (unlike the programming team) have people who may not be super technical, and Python let’s them write tools and scripts more easily. They don’t even have to understand OO but you can say “copy this class template and implement these two methods” and they can write tools that “work” in the pipeline.
It’s honestly a godsend. Before the industry settled on Python, every program had its own proprietary scripting language and some were quite limited. Their C++ APIs are all different, of course. So now everyone just ships with a Python interpreter, you manage launching each app so you can control PYTHONPATH and you’re golden.
Most of my objects are actually dataclasses.
I like writing Python in a relatively Functional way most of the time, with lots of list and dict comprehensions.
Well, it’s more procedural than object-oriented because it’s easier to avoid object-oriented programming than procedural code :D
(Note: I wouldn’t call defining classes OOP until you start using inheritance. Overriding
__str__
and stuff might count, but not a lot to me.)Personally, as time goes on, I use inheritance less.
You can write Python in an OOP style if you want to, and you sort of have to in the case of using certain libraries, but it’s mostly optional and imho rather ugly.
Inheritance sucks, and it’s especially bad in Python.
Neither: it’s a piece of shit.
If I had a gun to my head and had to answer, it’d be procedural because scopes are a lie in this language.
I like the cut of your jib
Python is good if you need to write a hundred or two or maybe three lines of code.
Its not good for large programs.
Conversely its good for large systems. Facebook makes all its microservices in python because the data model and how that scales along with data safety matters much more than a microservice taking 10-100 times as long to execute.
I consider it a scripting language. Just one without integer++ for some goddamn reason.
Python is good if you need to write a hundred or two or maybe three lines of code
Sooooo, any language?
My issue with Python is that so many “pythonic” practices lead to horrible-to-read code. List comprehensions are nice, except for when they go more than one level deep. kwargs leads to horrible code. Lots of valid Python code cannot be typed correctly, which makes editor inference far worse. Don’t get me started on metaclasses and the like.
Python right now is basically what JavaScript was before TS.
I used to struggle with OOP because all the explanations I saw were in terms of metaphors of real world objects. Once i started seeing OOP as a way to better structure your code, it all fell into place. If you do anything marginally complex, OO is the way to go.
I disagree. You can write a lot of high quality Python code (yeah it exists) before you need to use inheritance. If you’re reaching for inheritance as the solution to all complexity, GoF-style, then you’re doing it wrong.
It’s an occasionally useful tool that has its place, not something you should instinctively reach for.
OOP not just inheritance. Object oriented programming starts with structuring things in objects, like the name suggests. It quickly has a place in anything more then a few hundred lines of code.
I rarely use inheritance. Like i said, I see OOP mainly as a way to achieve cleaner code structure and better readability. That last point is really my main concern.
Ah yeah I agree. Misread your comment.
I do agree except for the last sentence because I do not think that OOP is the silver bullet for everything.
I was talking about Python specifically. And no, of course it’s not a silver bullet. It’s a solution for structuring your code base in a way that lets you not lose track of what does what.
The question is invalid!
Write the code that makes sense for your problem. This is not religion where choosing the wrong thing can land you in hell for eternity or whatever it is your gods will do if they don’t like you. You should be mixing OO, functional, and procedural code all the time as each does some things well and some things poorly. Of course don’t create a mess by the mix, but good code has a need for all 3 styles. (IIRC there are more styles the good code needs, but I can’t think of what it might be at the moment)
I love the almost spiritual nature of software development. It sounds crazy but the best devs I have ever worked with all immediately understood this perspective or had their own version of it.
The way we have these three categories of programming styles… off in one rhetorical direction, we see millions of little threads, innumerable individual languages, syntaxes, little styles… in the other direction? A monadic unity. All three categories are programming languages, which are just… forms of communication… which is just, well, shifting values of information. And that? Information? Formally it’s the potential for data but philosophically it is the noumena itself. The information world and the real world are one in the same.
I think people who are under the false impression that “everything has already been discovered” are so unfortunately blind to the beautiful world we have been endowed to discover, especially in contemporary times.
Could you elaborate on how information is the noumena?
Computers only have one language and one data type. Everything else is a construct, which we can build up into a beautiful thing: a mathematics that exists in the real world instead of the pure realm of axioms and symbols, and because it’s purely our own creation and not the universe’s, we know it from the fundamentals and don’t have to struggle with all the unknowns of physics, which presents us with very different, more mysterious mathematical objects to interact with instead.
For me it depends on the use case. If I’m designing something with an interface for someone downstream to use, I’ll usually define (data)classes even if I have a functional interface.
For data science/modeling/notebooks I usually wouldn’t define classes.
I think it also depends on your team; if everyone else is a functional programmer and you’re writing classes or vice versa, this will undoubtedly create frictions.
I use python for data sciences (kinda) and never write any classes or something object oriented, but given that dataframes are the bread and butter of everything I do, I guess I work in a object oriented workflow?
Yeah that’s a great point – the dataframe is in a sense a class or object standardized for data analysis. Its flexibility (like being able to store arrays or dicts even) obviates the need in most cases for a user-written class.
Any language can be procedural if you insist (maybe Haskell will fight you?), creating objects is a design choice to organize the code better. Any project beyond a few hundred lines of code should probably use objects.
The language itself can be considered object oriented since it allows the typical OOP patterns.
Obviously (almost) in any language code can be forcefully programmed the way you want, yes. But that is not how we normally code now is it? Usually languages & their community lean more towards certain paradigms than others.
Depends also on what you use python for. If you build a whole piece of software, you will be using objects. But many teams use Java for the business logic and python as a scripting language to call some api, or automate a task. In those cases python will be used procedurally, as a nicer bash basically.
As with most script languages, you can hit the ground running in a very procedural sort of way, as you don’t even have to define a main entry point. You just start coding.
But Python certainly has an object model. If I’m not mistaken, everything in Python is an object. Like even functions.
I suppose there are some aspects of the class implementation that feel a little tacked on? Like the way you need to manage the self reference manually where it may be implicitly handled for you in other languages. At least the way you call
super()
now is a lot less kludgy.One thing I miss a bit in Python is method overloading. In a general sense, function overloading is not an OOP feature per se, but I find it useful in OOP, particularly with object initializers. You can sort of achieve it with
@functools.singledispatch
but it’s pretty janky. For initialization, I prefer keeping the__init__
method pretty rudimentary and writing factory functions to do more complex initializations. And with@dataclass
, you can forego writing an__init__
altogether if you do it that way.It supports both and depending on the situation its more procedural or more object oriented. Do you consider the standard library as part of the language and are you forced to use it? Then certainly the object oriented parts of the language would be forced to use and therefore it is object oriented. Or do you only evaluate the language and its features itself? In that case, Python supports object oriented programming with classes and objects, but you are not forced to use it.
Are we talking about the language features itself or the produced programs with it in the real world?
So regardless if you look at the language itself or the common standard library that most Python programs use, objects and classes are an part of the language. Even if you write simple procedural scripts, does not take away that Python itself is a more object oriented programming language.
The language supports both, so it really depends on the user. The standard library doesn’t have a clear preference, and uses both approaches, along with functional style.
So I’ll say both, and my preference is a hybrid of functional and procedural.
It’s whatever you want it to be, baby.