In [ ]:
methods = []
for item in dir(2):
if item.startswith('__') and item.endswith('__'):
methods.append(item)
print(methods)
In [ ]:
(2).__str__()
In [ ]:
str(2)
In [ ]:
(2).__pow__(3)
In [ ]:
2 ** 3
In [ ]:
import this
In [ ]:
from great_circle import (
CAN, JFK, LAX, SLC,
Point, Distance,
MagicPoint, MagicDistance)
In [ ]:
CAN, JFK, LAX, SLC
In [ ]:
# Initialize some non-magic objects
slc1 = Point(SLC)
slc2 = Point(SLC)
lax = Point(LAX)
jfk = Point(JFK)
can = Point(CAN)
# Initialize some objects with magic methods
m_slc1 = MagicPoint(SLC)
m_slc2 = MagicPoint(SLC)
m_lax = MagicPoint(LAX)
m_jfk = MagicPoint(JFK)
m_can = MagicPoint(CAN)
In [ ]:
# Both p1 and p2 were instantiated using SLC coordinates.
slc1.coordinates(), slc2.coordinates()
In [ ]:
slc1 == slc2
The identity operator is
returns true only if the id()
function on both objects are equal.
In [ ]:
hex(id(slc1)), hex(id(slc2))
In [ ]:
slc1 is slc2
In [ ]:
slc1.latitude == slc2.latitude and slc1.longitude == slc2.longitude
In [ ]:
def is_equal(self, p):
"""
Test for equality with point p.
"""
return self.latitude == p.latitude and self.longitude == p.longitude
In [ ]:
slc1.is_equal(slc2)
In [ ]:
m_slc1 == m_slc2
__eq__()
, take a single argument, and return a boolean.
In [ ]:
slc1.calculate_distance(lax)
In [ ]:
jfk.calculate_distance(slc1)
In [ ]:
# __sub__() returns MagicDistance instance.
dist_lax_slc = m_slc1 - m_lax
dist_jfk_slc = m_slc1 - m_jfk
type(dist_lax_slc)
In [ ]:
# Jumping ahead a bit to representation of objects...
print(dist_lax_slc)
print(dist_jfk_slc)
In [ ]:
# Point
slc1
In [ ]:
# MagicPoint
m_slc1
In [ ]:
# MagicDistance
dist_lax_slc
In [ ]:
# Point
str(slc1)
In [ ]:
# MagicPoint
str(m_slc1)
In [ ]:
# MagicDistance
str(dist_lax_slc)
In [ ]:
# Point
"SLC coordinates: {}.".format(slc1)
In [ ]:
# Point
"SLC coordinates: {}.".format(slc1.coordinates())
In [ ]:
# MagicPoint
"SLC coordinates: {}.".format(m_slc1)
In [ ]:
# MagicPoint
"SLC coordinates: {:.4f}.".format(m_slc1)
In [ ]:
# MagicDistance
"Distance from LAX to SLC is {} nautical miles.".format(dist_lax_slc)
In [ ]:
# MagicDistance
"Distance from JFK to SLC is {} nautical miles.".format(dist_jfk_slc)
In [ ]:
dist_jfk_slc == dist_lax_slc
In [ ]:
dist_jfk_slc > dist_lax_slc
In [ ]:
dist_lax_slc >= dist_lax_slc
In [ ]:
dist_lax_slc(slc1, jfk)
In [ ]:
# No magic methods.
slc1 = None
In [ ]:
# Magic methods.
m_can = None