45 AnswersNew Answer
@Bagon Being able to to directly access the 'properties' of classes goes against OOP philosophy. Doing things like spam.eggs = ham is not using OOP, that's using a data structure, and it violates the whole purpose of OOP. There's nothing wrong with using data structures like this, that's why C/C++ have them. It just isn't OOP. Now I ain't bashing Python for the sake of it. As a general purpose multiparadigm scripting language it's got its good points. Just please don't act like it's the 'Holy Grail' of programming. Pun intended.
Martin Taylor you are a prophet?! "Dog Pile!" 😂 abderrahim I am still wrapping my head around OOP in python. I did not like the SL section on OOP and if you look at the section comments in the python tutorial you will realize that you are not alone. Check out this youtube playlist. I found it much more understandable: https://www.youtube.com/playlist?list=PL-osiE80TeTsqhIuOqKhwlXsIBIdSeYtc
(1)Imagine that you need to make a program with 10 bots. Without OOP, you will make your own variables and functions for each bot, you will write the same large piece of code 10 times, if something needs to be changed, you will have to look for what you need in each bot and change it, if you want to add something, you need to do this for each bot, etc., and working with a difficult program or large code, this is almost impossible. And thanks to OOP, you can make, you can say, a bot template, and make 10 instances from it - each will have its own variables, its own functions, despite the fact that you wrote the code only once and that all these variables and functions are called the same , then you can easily call the necessary functions of each bot. You can also configure the function, that is, what it will be - it will affect one specific bot, or all at once, and possibly not one of them. In general, it’s difficult, but very useful, I’m doing a Mafia game in python right now (suddenly, who doesn’t know
Martin Taylor, as a Python fan, I want to publicly say: Python's OOP is lacking. In the beginning, I didn't understand that, but over time it sank in. 😂 In my own short words, an object is supposed to protect the data it contains, and define *from the inside* what can be done with that data, and what is forbidden, by providing methods. If I can access anything from anywhere anyway, the object is effectively not protecting its data. One question though: You wrote, Python is not strongly typed. Is that correct? Python is not statically typed, so you can basically pass everything as an argument, and the function has to sort out if that is even legit. It's dynamically typed, so a variable name is not forever bound to one type of value, but it's just a 'sticker' that can be pasted onto everything. Strongly typed, however, means, that one object can not be arbitrarily read as something completely different - which C for example allows. At least that's how I've mostly seen it defined.
I agree with Martin Taylor. While python can be used for OO programming, it does not offer the best experience. In OOP, there should be a means to hide data from other classes. Python's does not have a `private` keyword and even if you prefix an attribute with two underscores it can still be called outside the class smh Also, in other OO languages, you don't need to explicitly pass the instance when calling the instance method. IMO Python does not deliver the best OOP developer experience
Just to show what can be done with OOP in Python, which is what abderrahim actually wanted, take a look at this example of implementing a Rectangle class and a Circle class inherited from a Shape class. https://code.sololearn.com/cy00gwOieMK0/#py There's no error checking, or try...except blocks, to keep the code as easy to understand as possible. Edit: Pasted the wrong link, now corrected. P.S. I use a browser to access SoloLearn so I can't see comments in the code playground.
@Bilguun There still are many specialist languages around. There are also many different programming paradigms. While OOP has been around for a long time it only really went mainstream with C++. Likewise, functional programming languages such as scheme and LISP have been around for almost half a century but are not mainstream languages. The problem is a lot of these languages are designed for a specific purpose which makes them rather niche. Others were designed to impliment a pure form of some paradigm with no regard to practicality. The software industry is always looking for ways to reduce costs, code maintenance costs way more than code development, by preventing bugs rather than fixing them. Hence enforcing strong type checking, encapsulation, garbage collection etc. Perhaps this explains the popularity of Python. It kind of does everything, procedural, functional, and oop but doesn't strictly enforce any of it. It's like shooting from the hip, fun but not without risks.
So in situations (small code) where there is no need for robustness, Python looks short and easy. But as soon as you want to make really sure, every method has to start with a bunch of tests, like: If arg 1 is really an int and arg 2 is definitely a string and arg 3 is by no means blablabla...
Python is a terrible language for OOP. It doesn't encapsulate properly, there's virtually no hiding of internal data. It can mimic OOP well enough but it's implementation is a train wreck. OOP is a way of thinking in terms of objects. Objects have properties like size, colour, age, values etc. They also have methods, ways of doing things like changing the value or colour - getters and setters. For simple objects this can be easy to understand and easy to implement. The trick comes in getting objects to interact with each other though. Unfortunately there is no easy way to teach you a golden rule for when to do what. It sort of comes with experience. https://greengarageblog.org/6-pros-and-cons-of-object-oriented-programming The Pythonistas are going to dogpile on me now.
@Granger If you think there is no way to program a card game without OOP then you know very little about data structures and algorithms. The reason I say Python is not great for OOP is because it does not hide implimentation properly. Even if you make members private or superduper extra mangled private they can still be accessed without using methods, which goes against OOP ethos. Python isn't even a strongly typed language for goodness sake. Python, like every programming language, has its good and bad points, its strengths and weaknesses. Unfortunately, the Pythonistas refuse to accept that and down vote any criticism of their sacred language. It reminds me of the flame wars over vi and emacs back in the day. Python looks like the love child of lua and Tcl on crystal meth. How can you take a language named after a bunch of comedians seriously? Looking forward to the dog pile.
Bagon An important part of OOP is abstraction. A class should abstract away its implementation details(anything that is not significant to its use). The reasons are thus: 1. Changing the implementation details will alter the behaviour of the class and may cause it to longer behave has a member of its type. 2. If the implementation details are made public. The developer can't know how far the client's have tampered with the objects and will be forced to release new versions with extreme caution because they will likely break things.
@Bagon The reason for the privacy is not in the sense that it's private and confidential but to enforce encapsulation. OOP is about writing extremely robust and scalable code. We're talking about extremely large projects here with multiple programmer's working simultaneously on the code and working to a design document. That's why enterprise likes Java. It's a very formal language with very strict rules. Python's strong point is "We're all consenting adults here.". So you are just trusted to be responsible and do the right thing. However, look around at the world to see how responsible we adults are, and how good we are at sticking to the rules (law).
It's better read some in the book and then learn in sololearn because not everything is clear here
The ability to model real world objects.
The ability to reduce duplicate code.
Don't feel bad my guy, OOP is one of the worst sections of the Python course, just look in the comments and you'll see all the frustrations 😉 it helps a lot to take notes and consult them if you have trouble. I personally have a fat binder full of notebook paper called the "Compendium of Python Bullshit Vol. 1" 😂😂😂
Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.
@HonFu Python is dynamically typed, it figures out the data type at run time. Statically typed languages check the data type at compile time. In an interpreter that parses the code before executing it this would class as compile time. There's no officially sanctioned definition of strongly typed that I am aware of, it's just my lazy use of the term. When I think of something that is static I think of it as unchanging. Statically typed languages such as the C's and Java have a limited ability to convert one data type to another. For example an int can be promoted to a long, type casting, and auto boxing and unboxing, but you can't assign a string to an int. It's just my lazy use of the term strong instead of static. With statically typed languages method overloading is used to create methods of the same name that accept different types or numbers of arguments. This can't be done in Python. Worse still all methods are effectively overloaded to accept any data type but the same number of arguments. This means a lot more code has to be written to create robust classes.
before object oriented technology there were many programming languages for each. such as language for business programming language for mathematicians from the late 1960’s first OOP style programming language Simula has been developed.Since then Other different majority(mathematicians,businessman) can use OOP Style to program their software.might be Some kind of philosophy of Programming.
Think about classes like same in video games. But they're not a wizard and warrior, but pure enemy and hero. Also item and etc. OOP helps you to reduce amount of code to write, and gives a lot of helpful features (especially in C++ or even Java :)(.Net)