Creating a Class (Class Definition) : Class Definition « Class « Python Tutorial






class AddrBookEntry(object):           
    def __init__(self, nm, ph):        
        self.name = nm                 
        self.phone = ph                
        print 'Created instance for:', self.name
    def updatePhone(self, newph):      
        self.phone = newph
        print 'Updated phone# for:', self.name

john = AddrBookEntry('A', '000-555-1212')
jane = AddrBookEntry('B', '111-555-1212')
print john
print john.name
print john.phone
print jane.name
print jane.phone

john.updatePhone('111-555-1212')
print john.phone








11.9.Class Definition
11.9.1.Demonstrates a basic class and object
11.9.2.Creating a Class (Class Definition)
11.9.3.Classes and Types
11.9.4.Defining Class Methods with the def Statement
11.9.5.Rectangle class
11.9.6.Throwing Methods Around
11.9.7.Simple definition of class Time.
11.9.8.Definition of class Date.