CMU 15-110: Principles of Computing
Objects (Structs)
# We will use objects lightly in this course. This example
# captures everything you need to know about them:
# First, we must define the Struct class (don't forget this!):
class Struct(object):
pass
# Now we can create and use objects (Struct instances):
person1 = Struct() # person1 refers to an object or instance of Struct
person1.name = 'fred' # name is now a property of the object
person1.age = 32 # age is also now a property of the object
person2 = Struct() # person2 refers to a different object!
person2.name = 'wilma'
person2.age = 34
# We can use this in a function:
def printPersonInfo(person):
print(person.name, 'is', person.age, 'years old')
printPersonInfo(person1)
printPersonInfo(person2)