Classes.py

>>> class Worker: def __init__(self, name, pay): self.name = name self.pay = pay def lastName(self): return self.name.split()[-1] def giveRaise(self, percent): self.pay *= (1.0 + percent) # Initialize when created # self is the new object # Split string on blanks # Update pay in place >>> bob = Worker('Bob Smith', 50000) >>> sue = Worker('Sue Jones', 60000) >>> bob.lastName() 'Smith' >>> sue.lastName() 'Jones' >>> sue.giveRaise(.10) >>> sue.pay 66000.0 # Make two instances # Each has name and pay attrs # Call method: bob is self # sue is the self subject # Updates sue's pay
User-Defined Classes

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.