| | |
OOP for noobs
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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!
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!
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:
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.
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)
# a look at a simple Python class class Animal(object): # uses newer class style, inheriting very basic class 'object' def __init__(self, animal, sound): # __init__() is the 'constructor' of the class instance # when you create an instance of Animal you have to supply # it with the name and the sound of the animal # 'self' refers to the instance # if the instance is dog, then # name and sound will be that of the dog # 'self' also makes name and sound available # to all the methods within the class self.name = animal self.sound = sound def speak(self): # a method's arguments always start with self print( "The %s goes %s" % (self.name, self.sound) ) # create a few class instances # remember to supply the name and the sound for each animal dog = Animal("dog", "woof") cat = Animal("cat", "meeouw") cow = Animal("cow", "mooh") # now you can call each animals function/method speak() # by simply connecting instance_name and speak() with a '.' cow.speak() cat.speak() dog.speak() # you can also access variables associated with the instance # since cow is the instance # self.sound becomes cow.sound print(cow.sound) # --> mooh
No one died when Clinton lied.
![]() |
Similar Threads
- Is VB.NET an OOP? (VB.NET)
- What "Noobs" (IT Professionals' Lounge)
- OOP's and GUI's...oh my! (Computer Science)
- basics of OOP (C)
- minor problem with vectors and OOP (C++)
- oop (C)
Other Threads in the Python Forum
- Previous Thread: I open IDLE and I immediately get a Recurring Error
- Next Thread: Two commands from one button
| Thread Tools | Search this Thread |
alarm ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory drive dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo verify webservice wikipedia windows write wxpython xlib






