On Mon, Nov 25, 2013 at 12:07:47PM +0530, Reuben wrote: > Hi, > > > Question no 1: > ---------------------- > I would like to know why do we actually inherit classes? What would be the > benefit of inheriting? > > If possible, a practical example would be of great help
Fundamentally, inheritence is a way of re-using code. You've written a class to do something, to solve some problem, and now you need to solve a slightly different problem. If you can borrow code from the original class, 95% of the work is already done. You certainly don't want to just *copy and paste* the code from one class into another class, because then every time you fix a bug in one, you have to remember to fix it in the other as well. And then there will be a third, and a fourth, and so on. Trying to manage all those independent copies will be a nightmare. Instead, but using inheritence, you have one "master copy", the parent class, and then each subclass "inherits" code from the parent, changing only the bits they actually need to. Usually by adding new code, or by modifying existing code. Here's a good video about subclassing, by one of the top Python developers: http://www.youtube.com/watch?v=miGolgp9xq8 > Question no 2: > ---------------------- > > Why would I ever use a class? I understand this is strange question Because classes bring data and the code to work on that data together. Because classes help make code reusable and extendable. Because classes help avoid copy-and-paste programming. All the basic data types in Python are classes. Strings, ints, floats, lists, dicts... they're all classes. So you're already using classes. Why might you write your own class, instead of using an existing one? That's a good question. Sometimes the answer is, you shouldn't. http://www.youtube.com/watch?v=o9pEzgHorH0 This is Python, not Java, and there is no need to wrap every piece of functionality in a class: http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html Still, there are good reasons for writing your own classes: - you want to extend an existing class - you want something which is best understood as a set of data plus the functions to work with that data. - http://lucumr.pocoo.org/2013/2/13/moar-classes/ (warning: quite technical) Confused? Don't be. Classes are a tool. The ultimate aim is to write good programs. Sometimes you need a screwdriver, and sometimes you need a hammer. A good programming language comes with both screwdrivers and hammers. Let's start with a small toy class: class Parrot: def __init__(self, breed, colour, name="Polly"): # Create a new Parrot instance. self.breed = breed self.colour = colour self.name = name def talk(self): message = "%s wants a cracker!" % self.name print(message) def squawk(self): print("Squawk!!!") def describe(self): message = """\ You see a bird in a cage. It is a %s parrot. It has an intelligent look to its eyes, a very sharp beak, and beautiful %s plummage.""" print(message % (self.breed, self.colour)) Here the class expects three pieces of data, the breed of the parrot, its colour, and the name of the individual bird. The class also defines three methods (four, if you include __init__, which is used for initialising a new instance of the class). The "describe" method describes the bird. The "squawk" method isn't very exciting, but the "talk" method knows the bird's own name. Try it: py> polly = Parrot("Norwegian Blue", "blue") py> polly.talk() Polly wants a cracker! If we change the bird's name, the method automatically does the right thing: py> polly.name = "Jackie" py> polly.talk() Jackie wants a cracker! > May be an example to make me understand would be useful. > > Or may be answering the question should be rephrased as "Why not use > modules instead of classes?" Modules and classes are complementary, not in opposition. Classes live inside modules, you can't have a class without a module to put it in. -- Steven _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor