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.
Python
Welcome to the Python community on the programming.dev Lemmy instance!
📅 Events
Past
November 2023
- PyCon Ireland 2023, 11-12th
- PyData Tel Aviv 2023 14th
October 2023
- PyConES Canarias 2023, 6-8th
- DjangoCon US 2023, 16-20th (!django 💬)
July 2023
- PyDelhi Meetup, 2nd
- PyCon Israel, 4-5th
- DFW Pythoneers, 6th
- Django Girls Abraka, 6-7th
- SciPy 2023 10-16th, Austin
- IndyPy, 11th
- Leipzig Python User Group, 11th
- Austin Python, 12th
- EuroPython 2023, 17-23rd
- Austin Python: Evening of Coding, 18th
- PyHEP.dev 2023 - "Python in HEP" Developer's Workshop, 25th
August 2023
- PyLadies Dublin, 15th
- EuroSciPy 2023, 14-18th
September 2023
- PyData Amsterdam, 14-16th
- PyCon UK, 22nd - 25th
🐍 Python project:
- Python
- Documentation
- News & Blog
- Python Planet blog aggregator
💓 Python Community:
- #python IRC for general questions
- #python-dev IRC for CPython developers
- PySlackers Slack channel
- Python Discord server
- Python Weekly newsletters
- Mailing lists
- Forum
✨ Python Ecosystem:
🌌 Fediverse
Communities
- #python on Mastodon
- c/django on programming.dev
- c/pythorhead on lemmy.dbzer0.com
Projects
- Pythörhead: a Python library for interacting with Lemmy
- Plemmy: a Python package for accessing the Lemmy API
- pylemmy pylemmy enables simple access to Lemmy's API with Python
- mastodon.py, a Python wrapper for the Mastodon API
Feeds
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.
It's also a good way to structure your code to effectively model your problem domain.
And there are also many cases where that's not true, so use it where it makes sense and don't use it where it doesn't.
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.
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?
yeah that’s definitely the boldest claim made in the OP comment. was surprised to see it hang out unchallenged for so long.
there’s been a growing trend to assign ontological primacy to information over mass/energy/space/etc. it’s a hot point for debate in computer science and physics research because, as my Kantian diction was kind of intended to imply: we can’t really empirically verify any ontological theories we have so we end up arguing over things that seem semantic from the outside in.
that means that, at least for now, “information is the noumena” is a matter of opinion. i just have strong, albeit potentially misplaced feelings - that mass, energy, and information are all different expressions of the same substrate that builds reality itself and that there is an undiscovered mechanic governing all three in a self-consistent manner.
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.
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.
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.
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.
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.
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.
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.
Python is good if you need to write a hundred or two or maybe three lines of code
Sooooo, any language?
It's whatever you want it to be, baby.