Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Martin Mareš
prm2
Commits
5d1d9b2e
Commit
5d1d9b2e
authored
Mar 01, 2020
by
Martin Mareš
Browse files
Typing
parent
4117cf42
Changes
3
Hide whitespace changes
Inline
Side-by-side
03-typing/03-typing.tex
0 → 100644
View file @
5d1d9b2e
\documentclass
{
beamer
}
\usepackage
[utf8]
{
inputenc
}
\usepackage
[czech]
{
babel
}
\usepackage
{
palatino
}
\usepackage
{
verbatim
}
\usetheme
{
Warsaw
}
\title
{
Programování 2: Typové anotace
}
\author
[Martin Mareš]
{
Martin Mareš
\\\texttt
{
mj@ucw.cz
}}
\institute
{
Katedra Aplikované Matematiky
\\
MFF UK Praha
}
\date
{
2020
}
\setbeamersize
{
text margin left=5mm
}
\setbeamersize
{
text margin right=5mm
}
\begin{document}
\setbeamertemplate
{
navigation symbols
}{}
\setbeamertemplate
{
footline
}{}
\setbeamerfont
{
title page
}{
family=
\rmfamily
}
\shorthandoff
{
"
}
\begin{frame}
\titlepage
\end{frame}
\input
../slidemac.tex
% ----------------------------------------------------------------------
\begin{frame}
{
Proč a nač?
}
Jak se programovací jazyky chovají k~typům dat:
\begin{itemize}
\item
{
\bf
statické
}
typování -- typ mají proměnné a funkce
\item
{
\bf
dynamické
}
typování -- typ mají hodnoty
\end{itemize}
~
Obojí má své výhody i nevýhody.
~
K~čemu třeba jsou užitečné statické typy:
\begin{itemize}
\item
Lepší dokumentace
\\
(např.: funguje
{
\bf
math.exp
}
na komplexní čísla?)
\item
Kontrola konzistence typů před spuštěním programu
\item
Napovídání v~editorech
\end{itemize}
~
Dnešní Python umí kombinovat statické a dynamické typy~
\dots
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Typové anotace
}
{
\tt\color
{
blue
}
def f(x: int, y: str) -> int:
\\
~~~~return x + len(y)
}
\smallskip
Čteme:
{
\bf
f
}
je funkce, parametr
{
\bf
x
}
je typu
{
\bf
int
}
,
{
\bf
y
}
typu
{
\bf
str
}
,
\\
vrací výsledek typu
{
\bf
int
}
.
~
{
\tt\color
{
blue
}
a: int = 42
}
\smallskip
Proměnné
{
\bf
a
}
jsme deklarovali typ
{
\bf
int
}
.
~
Python sám typy nekontroluje (to dělají samostatné type checkery),
ale pamatuje si je:
\smallskip
\py
{
%
f.
\_\_
annotations
\_\_
}{
%
\{
'x': int, 'y': str, 'return': int
\}
}
\py
{
%
help(f)
}{
%
Help on function f in module
\_\_
main
\_\_
:
\\
f(x: int, y: str) -> int
}
\end{frame}
% ----------------------------------------------------------------------
\begin{frame}
{
Složitější typy
}
{
\tt\color
{
blue
}
from typing import List
\\
def g(seznam: List[str]) -> None:
}
\smallskip
Parametrizovaný typ pro seznamy.
~
{
\tt\color
{
blue
}
from typing import Optional
\\
def center(s: str, šíře: Optional[int]=80) -> str:
}
\smallskip
Buď
{
\bf
int
}
nebo
{
\bf
None
}
(též chybí-li parametr při volání).
~
{
\tt\color
{
blue
}
Class Třída:
\\
~~~~def metoda(self, něco: int):
}
\smallskip
{
\bf
self
}
automaticky dostane typ
{
\bf
Třída
}
.
~
{
\tt\color
{
blue
}
from typing import Sequence, TypeVar
\\
T = TypeVar("T")
\\
def choose(s: Sequence[T]) -> T:
}
\smallskip
Funguje pro jakoukoliv homogenní posloupnost (seznam/tuple/
\dots
).
\end{frame}
% ----------------------------------------------------------------------
\end{document}
03-typing/Makefile
0 → 100644
View file @
5d1d9b2e
SLIDES
=
03-typing.pdf
include
../Makerules
0
3
-matplotlib/notes
→
0
4
-matplotlib/notes
View file @
5d1d9b2e
File moved
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment