Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ic:resurse:python [2021/10/04 10:42]
razvan.smadu
ic:resurse:python [2023/10/02 00:10] (current)
razvan.smadu [Python3 Crash Course]
Line 257: Line 257:
 r1 = random.random() r1 = random.random()
 </​code>​ </​code>​
 +
 +=== Type Hints ===
 +
 +Python este un limbaj tipat dinamic. Pentru a ușura întelegerea codului, putem folosi type hints care reprezintă adnotări asupra tipurilor de date. Acestea nu sunt forțate la runtime, fiind responsabilitatea programatorului să se asigure că tipurile nu induc în eroare. Detalii legate de type hints se pot găsi în [[https://​docs.python.org/​3/​library/​typing.html|documentație]],​ [[https://​peps.python.org/​pep-0484/​|PEP 484]] și [[https://​peps.python.org/​pep-0483/​|PEP 483]].
 +
 +Exemple:
 +<code python>
 +from typing import Dict, List, Sequence, Tuple, TypeVar
 +
 +my_string: str = "hello world" ​ # A variable storing a string
 +my_number: int = 10  # A variable storing an integer
 +
 +# A function returning nothing
 +def main() -> None:
 +    return ​ # Returns nothing (i.e., None)
 +
 +# A function that takes some arguments, and returns a tuple
 +def compute(a: int, b: float, c: List[str]) -> Tuple[int, float]:
 +    return a, b  # This is a tuple of `a` and `b`
 +
 +T = TypeVar('​T'​) ​ # Declare type variable
 +
 +def first(l: Sequence[T]) -> T:  # A generic function
 +    return l[0]
 +
 +# Type alias
 +MyCustomType = Dict[str, List[Tuple[int,​ int]]]
 +</​code>​
 +
  
 ==== Referințe ==== ==== Referințe ====
ic/resurse/python.1633333345.txt.gz · Last modified: 2021/10/04 10:42 by razvan.smadu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0