Skip to content
Snippets Groups Projects
Commit 55b6394a authored by jkvis's avatar jkvis
Browse files

Updated for Python 3

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