Commit 55b6394a authored by jkvis's avatar jkvis
Browse files

Updated for Python 3

parent 084e3e4a
No preview for this file type
......@@ -192,7 +192,7 @@ create new instances of a class.
\begin{minted}{python}
origin = Vector()
v1 = Vector(2, 1)
print v1.x, origin.y
print(v1.x, origin.y)
\end{minted}
\bigskip
......@@ -224,7 +224,7 @@ class Vector(object):
\item we agree not to access this attribute directly:
\begin{minted}{python}
>>> v1 = Vector(1, 2)
>>> print v1._secret # this is forbidden
>>> print(v1._secret) # this is forbidden
\end{minted}
\end{itemize}
......@@ -254,7 +254,7 @@ origin.distance(v1)
\begin{frame}[fragile]{Print representation of an object}
\begin{minted}{python}
>>> v1 = Vector(4, 3)
>>> print v1
>>> print(v1)
<__main__.Vector object at 0x7f41ab878450>
\end{minted}
......@@ -267,7 +267,7 @@ origin.distance(v1)
when using the \mintinline{python}{print} function;
\item we are in control of what is printed, e.g., for the vector class:
\begin{minted}{python}
>>> print v1
>>> print(v1)
<4, 3>
\end{minted}
\end{itemize}
......@@ -297,7 +297,7 @@ class Vector(object):
\item get the type of an instance:
\begin{minted}{python}
>>> v1 = Vector(4, 3)
>>> print type(v1)
>>> print(type(v1))
\end{minted}
%% start minted fix
\vspace{-.08cm}
......@@ -306,20 +306,20 @@ class Vector(object):
\item that also works for the class:
\begin{minted}{python}
>>> print type(Vector)
>>> print(type(Vector))
<type 'type'>
\end{minted}
\item use \mintinline{python}{isinstance()} to check if an object is a
vector:
\begin{minted}{python}
>>> print isinstance(v1, Vector)
>>> print(isinstance(v1, Vector))
True
\end{minted}
\item what happens here:
\begin{minted}{python}
>>> print v1.distance(4)
>>> print(v1.distance(4))
\end{minted}
\end{itemize}
\end{frame}
......@@ -328,7 +328,7 @@ True
\begin{itemize}
\item define special operators like:
\mintinline{python}{+,-,==,<,>,len(),...}
see: \path{https://docs.python.org/2/reference/datamodel.html#basic-customization}
see: \url{https://docs.python.org/3/reference/datamodel.html#basic-customization}
\item these can be \textcolor{pms280_compl}{overloaded} to work with your
class (keep it sensible);
......@@ -357,7 +357,7 @@ class Vector(object):
\begin{minted}{python}
>>> v1 = Vector(1, -6)
>>> v2 = Vector(3, 4.5)
>>> print v1 + v2
>>> print(v1 + v2)
<4, -1.5>
\end{minted}
\end{frame}
......@@ -415,8 +415,8 @@ containing two integers: \mintinline{python}{numerator} and
\bigskip
see: \path{https://github.com/lumc-python/oop} \\
or go directly to: \textcolor{pms280_compl}{\path{https://classroom.github.com/a/8BnbL9fD}}
see: \url{https://github.com/lumc-python/oop} \\
or go directly to: \textcolor{pms280_compl}{\url{https://classroom.github.com/a/8BnbL9fD}}
\end{frame}
\end{document}
No preview for this file type
......@@ -28,7 +28,7 @@
\author{Jonathan~K.~Vis}
\institute[LUMC]{Dept. of Human Genetics, Leiden University Medical Center}
\date{September 21st, 2017}
\date{}
\title{Solutions --- Object Oriented Programming}
\begin{document}
......@@ -75,7 +75,7 @@ class Fraction(object):
\texttt{test\_fraction.py}
\begin{minted}{python}
>>> print Fraction(1, 2)
>>> print(Fraction(1, 2))
1/2
\end{minted}
\end{frame}
......@@ -102,7 +102,7 @@ class Fraction(object):
\texttt{test\_fraction.py}
\begin{minted}{python}
>>> print Fraction(1, 2) + Fraction(2, 3)
>>> print(Fraction(1, 2) + Fraction(2, 3))
7/6
\end{minted}
\end{frame}
......@@ -130,7 +130,7 @@ class Fraction(object):
\texttt{test\_fraction.py}
\begin{minted}{python}
>>> print Fraction(1, 2).invert()
>>> print(Fraction(1, 2).invert())
2/1
\end{minted}
\end{frame}
......@@ -151,14 +151,14 @@ class Fraction(object):
return Fraction(self._denominator, self._numerator)
def to_float(self):
return float(self._numerator) / float(self._denominator)
return self._numerator / self._denominator
\end{minted}
\vfill
\texttt{test\_fraction.py}
\begin{minted}{python}
>>> print Fraction(1, 2).to_float()
>>> print(Fraction(1, 2).to_float())
0.5
\end{minted}
\end{frame}
......@@ -174,17 +174,17 @@ class Fraction(object):
return Fraction(self._denominator, self._numerator)
def to_float(self):
return float(self._numerator) / float(self._denominator)
return self._numerator / self._denominator
def integer(self):
return int(self._numerator) / int(self._denominator)
return self._numerator // self._denominator
\end{minted}
\vfill
\texttt{test\_fraction.py}
\begin{minted}{python}
>>> print Fraction(7, 6).integer()
>>> print(Fraction(7, 6).integer())
1
\end{minted}
\end{frame}
......@@ -197,10 +197,10 @@ class Fraction(object):
...
def to_float(self):
return float(self._numerator) / float(self._denominator)
return self._numerator / self._denominator
def integer(self):
return int(self._numerator) / int(self._denominator)
return self._numerator // self._denominator
def __sub__(self, other):
return Fraction(self._numerator * other._denominator -
......@@ -212,7 +212,7 @@ class Fraction(object):
\texttt{test\_fraction.py}
\begin{minted}{python}
>>> print Fraction(1, 2) - Fraction(2, 3)
>>> print(Fraction(1, 2) - Fraction(2, 3))
-1/6
\end{minted}
\end{frame}
......@@ -226,7 +226,7 @@ class Fraction(object):
...
def integer(self):
return int(self._numerator) / int(self._denominator)
return self._numerator // self._denominator
def __sub__(self, other):
return Fraction(self._numerator * other._denominator -
......@@ -242,7 +242,7 @@ class Fraction(object):
\texttt{test\_fraction.py}
\begin{minted}{python}
>>> print Fraction(1, 2) * Fraction(2, 3)
>>> print(Fraction(1, 2) * Fraction(2, 3))
2/6
\end{minted}
\end{frame}
......@@ -271,7 +271,7 @@ class Fraction(object):
\texttt{test\_fraction.py}
\begin{minted}{python}
>>> print Fraction(1, 2) / Fraction(2, 3)
>>> print(Fraction(1, 2) / Fraction(2, 3))
3/4
\end{minted}
\end{frame}
......@@ -290,8 +290,8 @@ class Fraction(object):
def simplify(self):
divisor = gcd(self._numerator, self._denominator)
self._numerator = self._numerator / divisor
self._denominator = self._denominator / divisor
self._numerator = self._numerator // divisor
self._denominator = self._denominator // divisor
return self
\end{minted}
......@@ -299,7 +299,7 @@ class Fraction(object):
\texttt{test\_fraction.py}
\begin{minted}{python}
>>> print Fraction(2, 6).simplify()
>>> print(Fraction(2, 6).simplify())
1/3
\end{minted}
\end{frame}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment