Objects
Contents
Objects¶
date
&datetime
methods & attributes
Clicker Question #1¶
What would be the best way to store a date?
A) A string
B) A list of numbers
C) It can’t be done
D) A dictionary
E) Something else
Storing Dates¶
# A date, stored as a string
date_string = '29/09/1988'
print(date_string)
29/09/1988
# A date, stored as a list of number
date_list = ['29', '09', '1988']
date_list
['29', '09', '1988']
# A date, stored as a series of numbers
day = 29
month = 9
year = 1988
print(day)
29
# A date, stored as a dictionary
date_dictionary = {'day': 29, 'month': 9, 'year': 1988}
date_dictionary
{'day': 29, 'month': 9, 'year': 1988}
Objects¶
Ways to organize data (variables) and functions together.
Example Object: Date¶
# Import a date object
from datetime import date
date?
# Set the data we want to store in our date object
day = 29
month = 9
year = 1988
# Create a date object
my_date = date(year, month, day)
print(my_date)
1988-09-29
# Check what type of thing `my_date` is
type(my_date)
datetime.date
Accessing Attributes & Methods¶
.
, followed by the attribute/method name on the object.
Date - Attributes¶
Attributes look up & return information about the object.
attributes maintain the object’s state, simply returning information about the object to you
# Get the day attribute
my_date.day
29
# Get the month attribute
my_date.month
9
# Get the year attribute
my_date.year
1988
Date - Methods¶
These are functions that belong to and operate on the object directly.
methods modify the object’s state
# Method to return what day of the week the date is
my_date.weekday()
3
# Reminder: check documentation with '?'
date.weekday?
# Get the ISO format representation of the date (as a string)
my_date.isoformat()
'1988-09-29'
# operates on date object
# returns a string
type(my_date.isoformat())
str
It’s also possible to carry out operations on multiple date objects.
# define a second date
my_date2 = date(1980, 7, 29)
print(my_date, my_date2)
1988-09-29 1980-07-29
# calculate the difference between times
time_diff = my_date - my_date2
print(time_diff.days, "days") #in days
print(time_diff.days/365,"years") #in years
2984 days
8.175342465753424 years
Listing Attributes & Methods : dir
¶
# tab complete to access
# methods and attributes
my_date.
# works to find attributes and methods
# for date type objects generally
date.
## dir ouputs all methods and attributes
## we'll talk about the double underscores next lecture
dir(my_date)
['__add__',
'__class__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__ne__',
'__new__',
'__radd__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rsub__',
'__setattr__',
'__sizeof__',
'__str__',
'__sub__',
'__subclasshook__',
'ctime',
'day',
'fromisoformat',
'fromordinal',
'fromtimestamp',
'isocalendar',
'isoformat',
'isoweekday',
'max',
'min',
'month',
'replace',
'resolution',
'strftime',
'timetuple',
'today',
'toordinal',
'weekday',
'year']
Clicker Question #2¶
Given the code below:
my_date = date(year = 1050, month = 12, day = 12)
Which is the best description:
A)
my_date
is an object, with methods that store data, and attributes that store proceduresB)
my_date
is variable, and can be used with functionsC)
my_date
is an attribute, with methods attached to itD)
my_date
is a method, and also has attributesE)
my_date
is an object, with attributes that store data, and methods that store procedures
Clicker Question #3¶
For an object lets
with a method do_something
, how would you execute that method?
A)
do_something(lets)
B)
lets.do_something
C)
lets.do_something()
D)
lets.do.something()
E) ¯\_(ツ)_/¯
Clicker Question #4¶
For an object lets
with an attribute name
, how would you return the information stored in name
for the object lets
?
A)
name(lets)
B)
lets.name
C)
lets.name()
D) lets.get.name()
E) ¯\_(ツ)_/¯
Objects Example: DateTime¶
from datetime import datetime
# get documentation
datetime?
# Get the current date & time
now = datetime.today()
print(now)
2020-05-04 08:28:31.305005
# Check some attributes of the object
print(now.year)
print(now.day)
2020
4
# Check a method of the object
print(now.weekday())
0
Objects Summary¶
Objects allow for data (attributes) and functions (methods) to be organized together
methods operate on the object type (modify state)
attributes store and return information (data) about the object (maintain state)
dir()
returns methods & attributes for an objectSyntax:
obj.method()
obj.attribute
date
anddatetime
are two types of objects in Python