On 22/10/2013 10:25, Sven Hennig wrote: > Hello, I would like to learn a programming language and have decided to use > Python. I have some programming experience and doing well in Python. What > really causes me problems is OOP. > I'm just dont get it... I'm missing a really Practical example. In every > book I've read are the examples of such Class Dog and the function is bark. > Has > anyone an OOP example for me as it is really used in real code, so I can > better understand the concept? I do not know why this is so hard for me. >
What you may not realize is you're already doing OOP, just by using the standard library. When you open a file (or many other things that can produce a stream of bytes), you get an instance of class file. When you use that instance, you're calling methods of that instance. So when you say: infile = open("myfile.txt,"r") data = infile.readline() you're doing object oriented programming. You don't have to know what kind of thing "infile" is, you just have to know it has methods read(), readline(), close(), etc. When you want to write your own classes, or when you want to make a new class that's related but different from one of the thousands that are standard, that's when it gets interesting. As Alan says, GUI is one place where you'll be wrting your own classes, usually by deriving from one of the GUI library classes. At its most fundamental, a class is a description of how to create and how to manipulate instances. An instance has methods (functions), and attributes (data). When one class is derived from another, it can share some or most of the attributes and behavior of the parent class, but make changes. This helps avoid duplicating code when two things are similar. You're familiar with list and tuple. Those are built-in collection classes, supported explicitly by the language. But if you want your own collection, you may want to make a class for it. The Dog bark() example may seem silly, but a Dog has lots of other methods besides that one, and has lots of attributes (color, breed, health state, owner, etc.). In a sense those attributes are like a list within the Dog, but you want them to have nice names, instead of remembering that the 3rd one is owner. -- DaveA _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor