Not Getting Oop [python]
- Peaceblossom
-
Peaceblossom
- Member since: Dec. 23, 2003
- Offline.
-
- Forum Stats
- Member
- Level 25
- Reader
I mean, I understand how it works, and how to implement it using Python, however I don't understand what kinds of problems one would solve using it.
So I present to you a challenge, Newgrounds. I want a reasonably simple problem that can only be solved using object oriented programming. Either that, or one that would take a significant amount of extra work to solve procedurally.
Any problem you pose to me, I will attempt to solve in an object oriented fashion, unless I can think of a simpler way to solve it procedurally. This should really help me wrap my head around the idea of it. I also start classes on Monday where we are learning to solve problems with objects and classes in Python. If I can get a reasonably good head-start in that class I should be golden.
Also, if you could explain it's practicality in solving real world programs I would be greatly appreciative.
- Montycarlo
-
Montycarlo
- Member since: Mar. 14, 2005
- Offline.
-
- Forum Stats
- Member
- Level 24
- Blank Slate
OOP's core concepts are polymorphism, code reuse and organisation (Code elegance).
Instead of pretty much duplicating code with procedural, I can extend a class and add or change behavior with class hierarchy trees.
Try and create a system that handles some store stock; specifically a 'drinks' area.
We have the base class, a product.
Then a 'drinks' subclass extends Product, that adds some properties (Such as size in ml)
Softdrinks and alcohol then both subclass the drinks class.
Then a class for each softdrink and each bottle of alcohol (Although you may wish to further subclass alcohol to support Wines, Beers etc)
Because the softdrink, alcohol and their descendant classes extend from drink, they have default properties that they inherit. Hence, we can create polymorphic code, that takes a drink or product subclass instance and doesn't care what type it is, as long as it has the drink class interface. It may display some measurements on a GUI about the content (In ml's) or the price (Inherited from the product class).
Although practicality beats purity.
Errors should never pass silently.
In the face of ambiguity, refuse the temptation to guess.
- CronoMan
-
CronoMan
- Member since: Jul. 19, 2004
- Offline.
-
- Forum Stats
- Member
- Level 06
- Blank Slate
At 1/8/10 03:52 AM, Peaceblossom wrote: Any problem you pose to me, I will attempt to solve in an object oriented fashion, unless I can think of a simpler way to solve it procedurally.
Try plugin-support
"no sound in ass"
- Peaceblossom
-
Peaceblossom
- Member since: Dec. 23, 2003
- Offline.
-
- Forum Stats
- Member
- Level 25
- Reader
So apparently my course is on C and Java, and not Python. All this work trying to figure out OOP in Python was a waste. I'll be back in about a month or so asking about OOP in Java though, I'm sure.
- gumOnShoe
-
gumOnShoe
- Member since: May. 29, 2004
- Offline.
-
- Send Private Message
- Browse All Posts (15,244)
- Block
-
- Forum Stats
- Member
- Level 15
- Blank Slate
At 1/11/10 03:27 PM, Peaceblossom wrote: So apparently my course is on C and Java, and not Python. All this work trying to figure out OOP in Python was a waste. I'll be back in about a month or so asking about OOP in Java though, I'm sure.
That makes more sense as Python is generally used to script, and often times that is procedural in nature.
The point of Object oriented code is mostly organization. You'll find out that you could do everything with procedural code, but when you share your code with other people, inherit code from others or are working with many many many people on a project, OOP makes everything uniform and makes it easier to pick up.
Its a design model and doesn't enable you to solve problems you couldn't solve procedurally.
Another variant of programming is Functional Programming where everything is a function that takes in variable and exports them and you never ever once save anything in a variable ever. The code is like a giant tree structure and it can be hard to understand if you aren't used to it.
OOP is an abstraction away from spaghetti code.
- Peaceblossom
-
Peaceblossom
- Member since: Dec. 23, 2003
- Offline.
-
- Forum Stats
- Member
- Level 25
- Reader
At 1/11/10 03:35 PM, gumOnShoe wrote: OOP is an abstraction away from spaghetti code.
What I was thinking while learning OOP is that I wouldn't be able to use it unless I was constantly maintaining or updating a piece of software that I had created. With the drinks example, I would think that using databasing software (MySQL, etc) would be a better choice, as it has already been developed. In that example I feel as though I'd be reinventing the wheel as a learning exercise.
- gumOnShoe
-
gumOnShoe
- Member since: May. 29, 2004
- Offline.
-
- Send Private Message
- Browse All Posts (15,244)
- Block
-
- Forum Stats
- Member
- Level 15
- Blank Slate
At 1/11/10 03:40 PM, Peaceblossom wrote:At 1/11/10 03:35 PM, gumOnShoe wrote: OOP is an abstraction away from spaghetti code.What I was thinking while learning OOP is that I wouldn't be able to use it unless I was constantly maintaining or updating a piece of software that I had created. With the drinks example, I would think that using databasing software (MySQL, etc) would be a better choice, as it has already been developed. In that example I feel as though I'd be reinventing the wheel as a learning exercise.
Communicating with a database such as MySQL is slower than having some data in main memory and on the heap. Especially when you get into more complicated programming.
I won't get into the details of how it works, but if you consider file reading, its much simpler in Java to have a File object which has methods to read and write from that file than it is to write code which continually has to interpret binary bits on your system and cast it into different data types. That's an example where the Language Java benefits from having objects, as does your code.
If you think of a more complicated system, like a home brew server, you have to keep track of many things concurrently:
Users (Including IP addresses, identifiers, current state, input / output streams, etc)
Server variables etc.
The structure of a Server is much simpler when a user joins and you just have to write
User myUser = new User(properties ...);
And then anything a user has to do you can work on.
Also, since all objects in Java are extensible, if you decide you want Strings to have additional properties which you plan on using over and over, then you can create a new class called Rope, which extends String.
Another situation where OOP is useful in Java is with applets, which you may be exposed to. Nearly every visual thing in Java is represented by an object and it becomes much easier to manage them if they are objects that you can store in one of the many Collection objects provided by Java.
Finally, there are speed incursions introduced by MySql. There are security concerns as well. Additionally, as Java programs can be distributed to very different locations via JNLP & executable jars & applets you may not always have access to a database.
- eerr
-
eerr
- Member since: Dec. 26, 2009
- Offline.
-
- Forum Stats
- Member
- Level 02
- Blank Slate
If a C program throws an error or an exception, what happens?
Inside Java programs, errors are easily caught by an object, and are therefore not allowed to pass.
if the object order ran like this:
chart
I/o.receiveinput()
chart.updatechart( )
I/o.getinput()
{
String s ="10/0"
return s.process;
}
and for whatever reason, s.process choked on a divide by zero error, the error would probably be caught inside I/o. Or many other errors that can result from I/O.
Also, Java has a very strong security that heavily uses the object system. Supposedly. File writing permissions work with the operating system underneath, amoung alot of other cooperative processes.
With C or C++ you can basically muck around or screw up parts of a computer you (shouldn't) have access to. You can scribble anywhere you dam want with a single pointer, though it's not a good idea.
Now as for object oriented in general, it is an organization strategy. You can make a very long program and not produce mysterious bugs. Or, more likely, track down said bugs with ease.


