Skip to content

Instantly share code, notes, and snippets.

@luisenriquecorona
Created April 18, 2019 17:38
Show Gist options
  • Save luisenriquecorona/6bb5870a1849fcca8973b87dbc93d846 to your computer and use it in GitHub Desktop.
Save luisenriquecorona/6bb5870a1849fcca8973b87dbc93d846 to your computer and use it in GitHub Desktop.
User-Defined Classes
>>> 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