Skip to content

Instantly share code, notes, and snippets.

@robinchew robinchew/person.py
Created Mar 10, 2016

Embed
What would you like to do?
# 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
You can’t perform that action at this time.