943,919 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 491
  • Python RSS
Aug 8th, 2009
0

OOP for noobs

Expand Post »
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..
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bsod1 is offline Offline
4 posts
since Aug 2009
Aug 8th, 2009
0

Re: OOP for noobs

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!
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Aug 8th, 2009
1

Re: OOP for noobs

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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Aug 9th, 2009
0

Re: OOP for noobs

thank you guys, its really great starting for me.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bsod1 is offline Offline
4 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: I open IDLE and I immediately get a Recurring Error
Next Thread in Python Forum Timeline: Two commands from one button





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC