[Tutor] Help with understanding classes
Alex Hall
mehgcap at gmail.com
Sat May 21 13:59:15 CEST 2011
On 5/21/11, Robert Sjöblom <robert.sjoblom at 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 at 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)
mehgcap at gmail.com; http://www.facebook.com/mehgcap
More information about the Tutor
mailing list