Created
March 10, 2016 14:38
-
-
Save robinchew/a2ba4b10fb9afa37c8b3 to your computer and use it in GitHub Desktop.
This file contains 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
# To run person.py, simply do the following on the command line: | |
# python person.py | |
# | |
# NOTE! | |
# This example just demonstrates how to do functions and class objects. | |
# It does NOT demonstrate which way is better. | |
############ | |
# Function # | |
############ | |
def create_person(name, age, is_staff=False): | |
return { | |
'name': name, | |
'age': age, | |
'is_staff': is_staff | |
} | |
def make_older(person): | |
person['age'] += 1 | |
person = create_person('John', 20) | |
print(person['name']) | |
print(person['is_staff']) | |
print(person['age']) | |
make_older(person) | |
print(person['age']) | |
print('--------separator--------') | |
######### | |
# Class # | |
######### | |
class Person(object): | |
def __init__(self, name, age, is_staff=False): | |
# Constructor | |
self.name = name | |
self.age = age | |
self.is_staff = is_staff | |
def make_older(self): | |
self.age += 1 | |
########## | |
# Object # | |
########## | |
person = Person('John', 20) | |
print(person.name) | |
print(person.is_staff) | |
print(person.age) | |
person.make_older() | |
print(person.age) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment