In [14]:
class Animal():  #simple class definition
    colour="brown" #all instances of class will be brown

bear=Animal
bear.colour
Out[14]:
'brown'
In [17]:
class Animal():
    colour="brown"
    #the init method is called when an instance is created
    def __init__(self, height): 
        self.height = height
    
elephant=Animal(300) #don't have to supply the self arg, just height
print(elephant.colour)
print(elephant.height)
brown
300
In [20]:
#why use "self"?
#1. to initialize instances of the class with certain attributes
#not just attributes within the class as a whole

class Animal():
    #take colour out of here
    def __init__(self, height, colour): #add the attribute here in the init method
        self.height = height
        self.colour = colour

fish=Animal(10, "pink")
print(fish.colour)
print(fish.height)
pink
10
In [22]:
#2. to refer to the instance in other methods

class Animal():
    def __init__(self, height, colour): #add the attribute here in the init method
        self.height = height
        self.colour = colour
    def growAnimal(self, amount):
        self.height += amount

crow=Animal(20, "black")
print(crow.height)
crow.growAnimal(10)
print(crow.height)
20
30
In [ ]: