April 14th, 2005, 01:39 PM
OOP(Don't get why?)
Okay I have been wondering why is OOP so famous (and good supposely)
Why is OOP used so much and why is it considered good.
I can code stuff like it and also organized without classes and things...
And I have a coding problem...
How do delete a line in a txt file
April 14th, 2005, 04:32 PM
OOP is a very popular and will be popular for a very long time from now, forum of programing. Almost every modern programing language is oo, if any new commers want to be even thought about they most be capible of oo.
The reasons oo is great is because it simplifies things and treats everything in a diff manner than normal programing does. With oo you can reuse code very easly. When i say reuse i mean within the same program and with other projects/programs. I know you probably know all about functions and such well Imagin that you can group a bunch of functions together that relie on eachother and work with eachother. Well for that to work you have to call the other sending all the premiters, and such. Then the other function has to know witch premiters are being sent and it can turn into a mess.
with OO it isn't like that. You set up a logical grouping of functions and veribles. Then you make the hole class in its hole. Then its done. no going back and switching things around. Then you go on to the next class make the hole thing then its done. Set up one single way for one class to talk to the other and its all work. No going back and testing this if statment to see if it will not cancel out the others. For premitors within the class you can just need self. witch will have all the curren't state of the veribles that are shared between everything. So you could have 3 things of the same class running all diff.
You could i suppose to everything with conventional ways of programing that you can do with oo, but that would be absolutly stupid. You can also make the same program in convetional maners that you could in OO but the OO would be supior because lets say you want to add more to the conventional app, you will spend at least 4 times as much time trying to make it work, possible rewriting half the application again. While with OO you just had to edit a few lines and add a few new methods, and your done.
Trust me, i use to be like you. I didn't understand why OO was so important, i actually thought it was dumb. It only took me to learn Java, a lang that forces you to use OO, for me to actually learn the importance of it. Once you use OO you will never want to code in any other manner.
Comments on this post
April 14th, 2005, 11:44 PM
Mostly agree, but:
OO isn't the be all and end all of programming styles - there's also logic programming (Prolog), functional programming (Haskell, ML, etc), aspect oriented programming (AspectJ)... and sometimes plain procedural programming is all that's required, particularly for short scripts.
Just as procedural programming allows code to be simplified through factoring out commonalities of code, OO allows code to be simplified through factoring out commonalities of interface and organised through the collection of behaviours for notional "things" eg:
Fairly simple... and certainly a lot shorter than the OO version:
def calculate_rectangle_area(length, width):
rectangles = [(5,15),
for rect in rectangles:
Ok ... so it looks like all OO gives you is a lot of extra typing, right?
def __init__(self, side):
self.side = side
def __init__(self, length, width):
self.length = length
self.width = width
def __init__(self, radius):
self.radius = radius
rectangles = [Rectangle(5,15),
for rect in rectangles:
But what if you want to have a list of different shapes - you then have to associate a type with data - the procedural version would be:
... no longer quite so simple compared with:
shapes = [("CIRCLE", (7)),
area_lookup = ["CIRCLE": calculate_circle_area,
for shape in shapes:
shape_type, dimensions = shape
area_func = area_lookup[shape_type]
... and what if you wanted to add in colours and patterns to your shape ... and a function to calculate the perimeter... and more shapes. And what if you wanted to then add a coordinate system... and overlap shapes to create new shapes. Suddenly using functions no longer seems so simple.
shapes = [Circle(7),
for shape in shapes:
OO allows you to simplify bookkeeping as your code grows by making you do a little more up-front work.
Last edited by hydroxide; April 17th, 2005 at 02:44 AM.
Reason: Fix bug in Circle.calculate_area() [*blush*]
April 15th, 2005, 10:05 AM
Okay I get the point now
But whats with deleting a line in a txt file...
April 15th, 2005, 03:14 PM
Originally Posted by monkeyman23555
the simplest way to delete a line in a file is to copy the file line by line to another file except the line you want to delete. When you are done, delete the old file and rename the new file to it. Depending on how you identify the line you want to delete, you could do it with unix tools such as grep or sed. If you are on a Windows machine then download cygwin and use a unix tool such as grep or sed.
This may take a while if the file is a billion lines long.
Dave - The Developers' Coach
April 16th, 2005, 06:07 AM
Max lines are about 2000
April 16th, 2005, 09:33 AM
your OO Circle doesn't work even after fixing the formula. I get this error ...
NameError: global name 'radius' is not defined
April 16th, 2005, 09:59 AM
Put a self. infront of the radius
It is defined as self.radius = radius
Meaning not radius but self.radius
Edit: This is my 100th post
Last edited by monkeyman23555; April 17th, 2005 at 01:14 PM.
April 17th, 2005, 07:56 AM
Thanks, looks like Hydroxide corrected it already.