Created
April 18, 2019 17:38
-
-
Save luisenriquecorona/6bb5870a1849fcca8973b87dbc93d846 to your computer and use it in GitHub Desktop.
User-Defined Classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment