OOP for noobs

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 4
Reputation: bsod1 is an unknown quantity at this point 
Solved Threads: 1
bsod1 bsod1 is offline Offline
Newbie Poster

OOP for noobs

 
0
  #1
Aug 8th, 2009
Hi guys, firs of all, im sorry for my bad english..

How can i learn OOP, its really hard and detailed. Firs of all, i dont know why i must use OOP.

Thanks..
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 231
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training

Re: OOP for noobs

 
0
  #2
Aug 8th, 2009
Welcome to DaniWeb bsod1,

OOP(Object Oriented Programming) is the order of the day! It is very useful as in it is very flexible as it allows many methods present in a class to be accessed via an object of that class, and so, there are innumerable things that you can do with a really good OOP language(such as Python).

OOP isn't that hard, once you learn the concepts, such as classes, methods and objects and the basics, things really start to get interesting.

Considering the fact that you have created this thread in the Python section, I am guessing that you want to start learning Python. Well, www.python.org is the best place to start. If you have doubts be sure to come here and post them, and we are here to help you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: OOP for noobs

 
1
  #3
Aug 8th, 2009
In Python most everything is an objects, so you are using OOP from the start.
If you talking about the use of classes, then basically you have another tool to organize larger programs.

You can collect functions that belong together under the class header. These function are commonly called methods of a class. This way you can create several different instances of the class, each containing the same methods. That is where the power comes in, since you don't have to rewrite the class every time. Python keeps track of which instance you are using. Here is an example:
  1. # a look at a simple Python class
  2.  
  3. class Animal(object):
  4. # uses newer class style, inheriting very basic class 'object'
  5. def __init__(self, animal, sound):
  6. # __init__() is the 'constructor' of the class instance
  7. # when you create an instance of Animal you have to supply
  8. # it with the name and the sound of the animal
  9. # 'self' refers to the instance
  10. # if the instance is dog, then
  11. # name and sound will be that of the dog
  12. # 'self' also makes name and sound available
  13. # to all the methods within the class
  14. self.name = animal
  15. self.sound = sound
  16.  
  17. def speak(self):
  18. # a method's arguments always start with self
  19. print( "The %s goes %s" % (self.name, self.sound) )
  20.  
  21.  
  22. # create a few class instances
  23. # remember to supply the name and the sound for each animal
  24. dog = Animal("dog", "woof")
  25. cat = Animal("cat", "meeouw")
  26. cow = Animal("cow", "mooh")
  27.  
  28. # now you can call each animals function/method speak()
  29. # by simply connecting instance_name and speak() with a '.'
  30. cow.speak()
  31. cat.speak()
  32. dog.speak()
  33.  
  34. # you can also access variables associated with the instance
  35. # since cow is the instance
  36. # self.sound becomes cow.sound
  37. print(cow.sound) # --> mooh
You can go further and let one class inherit another class. You can see that in larger programs this will help keep things organized and readable. For smaller programs you don't need to write a class, but is good practice.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 4
Reputation: bsod1 is an unknown quantity at this point 
Solved Threads: 1
bsod1 bsod1 is offline Offline
Newbie Poster

Re: OOP for noobs

 
0
  #4
Aug 9th, 2009
thank you guys, its really great starting for me.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC