Re: [Tutor] Help with understanding classes
Robert...I am similarly where you are stuck, a beginner with little insight, so my perspective might have some draw to it. Try this on for size - while watching a friend stencil Christmas decorations on living room windows last year, it dawned on me that a class is quite simply - a stencil. While using a Santa Claus stencil, I watched my friend employ different features in different places; a Santa Claus here, some reindeer there, a few pine bows in another spot, and each with different colored spray paint...and the light bulb came on. Each different thing she stenciled was an instance of the class (stencil) Santa Claus. For me, that was the magic moment. Very unprofessional, I'm sure, but, hey, so what?! From the virtual desk of Lowell Tackett --- On Sat, 5/21/11, Robert Sjöblom robert.sjob...@gmail.com wrote: From: Robert Sjöblom robert.sjob...@gmail.com Subject: [Tutor] Help with understanding classes To: tutor@python.org tutor@python.org Date: Saturday, May 21, 2011, 6:24 AM I'm trying to wrap my head around classes and their attributes, but am having a hard time doing so. The websites and books that I have consulted haven't been much help; most of them assume prior programming/oop experience, something I lack. From what I understand it's a blueprint, so each time you instantiate it you run whatever's in the __self__ part is assigned to the instance. What I'm wondering is how to access and change attributes in the instance (whatever it might be; a list, a specific value, a string...). I just can't seem to grasp the concept or syntax very well, and none of the sources I've found go to great lengths explaining them to someone as thick-headed as me. So if anyone could take the time to explain, or point me to sources to read, I'd be grateful Thanks in advance, and best regards, Robert S. Sent from my iPhone ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with understanding classes
That's pretty much all you need to know to start using objects. There's a lot more though: inheritance, class methods and static methods (as opposed to ordinary methods), properties, descriptors (advanced!), slots, and more. But one step at a time. Any questions, don't hesitate to ask! Wow, amazing explanations! However, in your post, you suggest you don't have prior *programming* experience? My bad; I meant prior programming experience outside Python (which consists of some BASIC from, oh, 20 years ago). Anyway, with the explanations I'm slowly getting my head around it all. I'm sure I'll be back with more questions as soon as I can formulate them. My biggest problem, which everyone (except this list) skimmed over was the __init__ and self parts. Now that I've had those explained to me, it's starting to make sense. Thanks to everyone, again. best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with understanding classes
I'm trying to wrap my head around classes and their attributes, but am having a hard time doing so. The websites and books that I have consulted haven't been much help; most of them assume prior programming/oop experience, something I lack. From what I understand it's a blueprint, so each time you instantiate it you run whatever's in the __self__ part is assigned to the instance. What I'm wondering is how to access and change attributes in the instance (whatever it might be; a list, a specific value, a string...). I just can't seem to grasp the concept or syntax very well, and none of the sources I've found go to great lengths explaining them to someone as thick-headed as me. So if anyone could take the time to explain, or point me to sources to read, I'd be grateful Thanks in advance, and best regards, Robert S. Sent from my iPhone ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with understanding classes
On Sat, 21 May 2011 08:24:34 pm Robert Sjöblom wrote: I'm trying to wrap my head around classes and their attributes, but am having a hard time doing so. The websites and books that I have consulted haven't been much help; most of them assume prior programming/oop experience, something I lack. Let's start with a simple question -- what is an object? In programming, an object is a thing that combines data and behaviour together in one handy package. Back in the Dark Ages *wink*, code and behaviour were always separate, so you would have to design a data structure in one place of your program, then write functions to manipulate it somewhere else. If you had two different data structures which needed similar actions, you had to jump through hoops to keep the functions for one separate from the functions of the other. But objects let you keep related pieces of code together, so they don't get in the way of unrelated code with the same name: class Artist: def draw(self): print(I splashed paint on the canvas until it looked like my mother on a rocking horse.) class GunSlinger: def draw(self): print(The stranger reached for his gun, but I was too quick for him and plugged him full of lead.) The function (actually method) called draw for Artists doesn't get over-ridden by the method with the same name for GunSlingers: whistler = Artist() wyatt_earp = GunSlinger() whistler.draw() I splashed paint on the canvas until it looked like my mother on a rocking horse. wyatt_earp.draw() The stranger reached for his gun, but I was too quick for him and plugged him full of lead. Here you see object code in action. You start with a class, which is something vaguely like a template. (It's actually nothing like a template, but that will do for now.) Before you can use the class, you normally have to instantiate it. Think of it like this: in the real world, car is a kind of machine, or if you prefer, a *class* of machine. But you can't just get into the abstract class of car and drive to the shops, you need an actual, concrete, physical car: an *instance* of the class. In object oriented programming (OOP), this is normally the same: you create an instance of the class, and then work with that. The instance gets its behaviour (and sometimes its data) from the class. The examples above are classes with behaviour only, no data or state. They never change. That's not very useful. Normally you want to store data in the class instance, sometimes in the class itself. Here's an example: class Paper: colour = white size = A4 This defines a class with no behaviour, only state. In this case, it has two attributes, colour and size. The values stored in the class are global defaults: all Paper instances share the same value. But you can give individual instances their own independent value by assignment: sheet = Paper() sheet.colour 'white' another_sheet = Paper() another_sheet.size = A5 # cut the paper in half another_sheet.colour = green # and paint it another_sheet.colour # check that the change is stored 'green' sheet.colour # and check that the other sheet is unchanged 'white' Normally though, you don't need or want to set default state that applies to all instances. In the above Paper example, it is harmless, but sometimes it can lead to bugs, so be careful with class attributes. So although attributes common to all instances of a class can sometimes be useful, generally people try to avoid them, and it is more common to put off defining the instance's attributes until the instance is created. For that, Python has a special method called __init__ (that's two underscores at the front and back). When you create an instance by calling Paper(), the __init__ method (if any) is automatically called by Python. class Paper: def __init__(self, colour=white, size=A4): self.colour = colour self.size = size Now the attributes no longer live inside the class, but inside the instance. Paper instances no longer share global state. Each instance gets its own independent attribute for colour and size, which you can specify when you create it: sheet = Paper(blue, Foolscap) sheet.colour 'blue' Now, I've skimmed over a couple of important things here. Firstly, syntax: in Python, the syntax for attribute access is with a dot: paper.size lets you retrieve the attribute. To set the attribute, you simply assign to it: paper.size = A3 Notice that dot is the same syntax used for accessing methods: whistler.draw() That's because in Python, methods are just another attribute, only you can call them with () syntax. This lets you do all sorts of cool and advanced things, like store a method somewhere to use it later: import random if random.random() 0.5: method = wyatt_earp.draw # no parentheses! else: method = whistler.draw # later... method() # call the method but I digress.
Re: [Tutor] Help with understanding classes
On 5/21/11, Robert Sjöblom robert.sjob...@gmail.com wrote: I'm trying to wrap my head around classes and their attributes, but am having a hard time doing so. The websites and books that I have consulted haven't been much help; most of them assume prior programming/oop experience, something I lack. From what I understand it's a blueprint, so each time you instantiate it you run whatever's in the __self__ part is assigned to the instance. What I'm wondering is how to access and change attributes in the instance (whatever it might be; a list, a specific value, a string...). Say we have an animal class: #open a new class class animal: #make the __init__, which is automatically called when a new instance of the class #is created; put initial setup things here def __init__(self, nameString): #nameString is assigned to this class's name variable self.name=nameString #this function, since it is in the class, can be called on an instance of the class def printName(self): print self.name #make an animal whose name is Fluffy: myDog=animal(Fluffy) #call the animal class's method on this instance myDog.printName() #Fluffy #now change the name: myDog.name=Rover #call the same method on the same instance: myDog.printName() #Rover I just can't seem to grasp the concept or syntax very well, and none of the sources I've found go to great lengths explaining them to someone as thick-headed as me. So if anyone could take the time to explain, or point me to sources to read, I'd be grateful The above is not too in-depth, but I am also not quite sure which parts of syntax or concepts you are stuck on. In the above example, what, specifically, does not make sense? Thanks in advance, and best regards, Robert S. Sent from my iPhone ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Have a great day, Alex (msg sent from GMail website) mehg...@gmail.com; http://www.facebook.com/mehgcap ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with understanding classes
Robert Sjöblom robert.sjob...@gmail.com wrote I'm trying to wrap my head around classes ...The websites and books that I have consulted ...assume prior programming/oop experience, something I lack. Alex, and especially Steven (great job BTW!,) have given you good explanations, provided you understand the basics of programming. However, in your post, you suggest you don't have prior *programming* experience? If that's the case, forget about OOP and classes for now and go learn how to write basic code up to the point where you are comfortable defining and using your own functions. Then come back to classes and OOP and it will all make a lot more sense. OTOH If you can already create and use functions then Alex and Steven's explanations should be useful and if you still have specific questions feel free to post them here. You can also try my OOP tutorial topic, but it does assume basic familiarity with creating functions. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with understanding classes
What a phenomenally clear explanation. Thank you very much, Steven. Robert Berman On 05/21/2011 07:49 AM, Steven D'Aprano wrote: On Sat, 21 May 2011 08:24:34 pm Robert Sjöblom wrote: I'm trying to wrap my head around classes and their attributes, but am having a hard time doing so. The websites and books that I have consulted haven't been much help; most of them assume prior programming/oop experience, something I lack. Let's start with a simple question -- what is an object? In programming, an object is a thing that combines data and behaviour together in one handy package. Back in the Dark Ages *wink*, code and behaviour were always separate, so you would have to design a data structure in one place of your program, then write functions to manipulate it somewhere else. If you had two different data structures which needed similar actions, you had to jump through hoops to keep the functions for one separate from the functions of the other. But objects let you keep related pieces of code together, so they don't get in the way of unrelated code with the same name: class Artist: def draw(self): print(I splashed paint on the canvas until it looked like my mother on a rocking horse.) class GunSlinger: def draw(self): print(The stranger reached for his gun, but I was too quick for him and plugged him full of lead.) The function (actually method) called draw for Artists doesn't get over-ridden by the method with the same name for GunSlingers: whistler = Artist() wyatt_earp = GunSlinger() whistler.draw() I splashed paint on the canvas until it looked like my mother on a rocking horse. wyatt_earp.draw() The stranger reached for his gun, but I was too quick for him and plugged him full of lead. Here you see object code in action. You start with a class, which is something vaguely like a template. (It's actually nothing like a template, but that will do for now.) Before you can use the class, you normally have to instantiate it. Think of it like this: in the real world, car is a kind of machine, or if you prefer, a *class* of machine. But you can't just get into the abstract class of car and drive to the shops, you need an actual, concrete, physical car: an *instance* of the class. In object oriented programming (OOP), this is normally the same: you create an instance of the class, and then work with that. The instance gets its behaviour (and sometimes its data) from the class. The examples above are classes with behaviour only, no data or state. They never change. That's not very useful. Normally you want to store data in the class instance, sometimes in the class itself. Here's an example: class Paper: colour = white size = A4 This defines a class with no behaviour, only state. In this case, it has two attributes, colour and size. The values stored in the class are global defaults: all Paper instances share the same value. But you can give individual instances their own independent value by assignment: sheet = Paper() sheet.colour 'white' another_sheet = Paper() another_sheet.size = A5 # cut the paper in half another_sheet.colour = green # and paint it another_sheet.colour # check that the change is stored 'green' sheet.colour # and check that the other sheet is unchanged 'white' Normally though, you don't need or want to set default state that applies to all instances. In the above Paper example, it is harmless, but sometimes it can lead to bugs, so be careful with class attributes. So although attributes common to all instances of a class can sometimes be useful, generally people try to avoid them, and it is more common to put off defining the instance's attributes until the instance is created. For that, Python has a special method called __init__ (that's two underscores at the front and back). When you create an instance by calling Paper(), the __init__ method (if any) is automatically called by Python. class Paper: def __init__(self, colour=white, size=A4): self.colour = colour self.size = size Now the attributes no longer live inside the class, but inside the instance. Paper instances no longer share global state. Each instance gets its own independent attribute for colour and size, which you can specify when you create it: sheet = Paper(blue, Foolscap) sheet.colour 'blue' Now, I've skimmed over a couple of important things here. Firstly, syntax: in Python, the syntax for attribute access is with a dot: paper.size lets you retrieve the attribute. To set the attribute, you simply assign to it: paper.size = A3 Notice that dot is the same syntax used for accessing methods: whistler.draw() That's because in Python, methods are just another attribute, only you can call them with () syntax. This lets you do all sorts of cool and advanced things, like store a method somewhere to use it later: import random if random.random() 0.5: method = wyatt_earp.draw # no