← Back to Python Course | Chapter 11: Advanced Python | Lesson 8 of 12

Python Type Hints

Type hints वे labels हैं जो बताते हैं कि किसी variable या function को किस तरह का value चाहिए, जैसे किसी जार पर sugar लिखना। Python इन्हें enforce नहीं करता, पर ये लोगों और tools को गलतियाँ पकड़ने में मदद करते हैं।
Syntax
python
def function_name(parameter: type) -> return_type:
    # body

variable: type = value

Type Hints क्या हैं?

Type hints, जो Python 3.5 में PEP 484 के ज़रिए आए, ऐसे annotations हैं जो code के असल चलने के तरीके को बदले बिना किसी variable या parameter के expected type को document करते हैं -- Python का interpreter runtime पर इन्हें अभी भी अनदेखा करता है।

इनकी असली कीमत पहले ही मिल जाती है: editors और IDEs इन्हें सटीक autocomplete के लिए इस्तेमाल करते हैं, और static checkers code चलाने से पहले ही type गलतियाँ पकड़ने के लिए।

उदाहरण: What are Type Hints?

python
def add(a: int, b: int) -> int:  # hints document expected types, ignored at runtime
    return a + b

print(add(2, 3))

Function Parameter Type Hints

किसी function signature को annotate करने का मतलब है हर parameter के लिए param: Type और def line के closing colon से ठीक पहले -> ReturnType लिखना।

यह किसी function के contract को एक नज़र में explicit बना देता है, यह जानने के लिए कि वह क्या expect करता है और क्या लौटाता है, बिना उसका implementation या docstring पढ़े।

उदाहरण: Function Parameter Type Hints

python
def greet(name: str) -> str:  # name must be a str, function returns a str
    return f"Hello, {name}"

print(greet("Alex"))

Floating-Point Variables के लिए Type Hinting

float या bool जैसे simple scalar hints शुरुआत करने की सबसे आसान जगह हैं, क्योंकि ये उन values के लिए intent document करते हैं जो अकेले bare variable name से अन्यथा अस्पष्ट होतीं।

ये runtime पर कुछ भी खर्च नहीं करते और numeric या flag-जैसे variables को बाद में code पढ़ने वाले किसी भी व्यक्ति के लिए तुरंत self-explanatory बना देते हैं।

उदाहरण: Type Hinting Floating-Point Variables

python
price: float = 19.99  # type hint documents this as a float
is_active: bool = True  # type hint documents this as a bool
print(price, is_active)

typing Module से Complex Types

simple scalars से आगे किसी भी चीज़ के लिए -- lists, dicts, tuples, या कई types के unions -- आप typing module से List, Dict, और Tuple जैसे generic types import करते हैं (या Python 3.9+ में सीधे built-in generics इस्तेमाल करते हैं, जैसे list[int])।

यह आपको 'strings की एक list' या 'नामों को ages से map करने वाला dict' जैसे structured shapes को सटीकता से व्यक्त करने देता है।

उदाहरण: Complex Types from typing Module

python
from typing import List, Dict

names: List[str] = ["Alex", "Sam"]  # a list of strings
ages: Dict[str, int] = {"Alex": 30}  # a dict mapping strings to ints
print(names, ages)

Type Hints के फ़ायदे

mypy जैसे tools आपके type hints पढ़ते हैं और program को कभी चलाए बिना आपके codebase में type mismatches के लिए static रूप से analyze करते हैं -- जैसे कि int की जगह string पास करना।

Deployment से पहले इन errors को पकड़ना खासकर बड़े codebases में बहुत कीमती है, जहाँ एक गलत type किसी दिखाई देने वाले bug का कारण बनने से पहले चुपचाप लंबे समय तक फैल सकता है।

उदाहरण: Benefits of Type Hints

python
def add(a: int, b: int) -> int:
    return a + b

# A type checker like mypy would flag this at analysis time:
# add("2", 3)
print(add(2, 3))
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.