Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prm2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Mareš
prm2
Commits
5d1d9b2e
Commit
5d1d9b2e
authored
5 years ago
by
Martin Mareš
Browse files
Options
Downloads
Patches
Plain Diff
Typing
parent
4117cf42
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
03-typing/03-typing.tex
+154
-0
154 additions, 0 deletions
03-typing/03-typing.tex
03-typing/Makefile
+3
-0
3 additions, 0 deletions
03-typing/Makefile
04-matplotlib/notes
+0
-0
0 additions, 0 deletions
04-matplotlib/notes
with
157 additions
and
0 deletions
03-typing/03-typing.tex
0 → 100644
+
154
−
0
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}
This diff is collapsed.
Click to expand it.
03-typing/Makefile
0 → 100644
+
3
−
0
View file @
5d1d9b2e
SLIDES
=
03-typing.pdf
include
../Makerules
This diff is collapsed.
Click to expand it.
0
3
-matplotlib/notes
→
0
4
-matplotlib/notes
+
0
−
0
View file @
5d1d9b2e
File moved
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment