Links
Comment on page
💥

… (ellipsis) kullanımı

Python ile ... (ellipsis) kullanımı
  • == eş değerlik kontrolünde Ellipsis
    ... # Ellipsis
    Ellipsis # Ellipsis
    ... is Ellipsis # True
    ... == None # False
  • pass ile aynı anlama gelir
    # style1
    def foo():
    pass
    # style2
    def foo():
    ...
    # both the styles are same
  • Any ile aynı anlamda kullanımı
    class flow:
    # (using "value: Any" to allow arbitrary types)
    def __understand__(self, name: str, value: ...) -> None: ...
    # <https://realpython.com/python-ellipsis/>
    from typing import Callable
    def add_one(i: int) -> int:
    return i + 1
    def multiply_with(x: int, y: int) -> int:
    return x * y
    def as_pixels(i: int) -> str:
    return f"{i}px"
    def calculate(i: int, action: Callable[..., int], *args: int) -> int:
    return action(i, *args)
    # Works:
    calculate(1, add_one)
    calculate(1, multiply_with, 3)
    # Doesn't work:
    calculate(1, 3)
    calculate(1, as_pixels)
  • None olarak kullanım
    def food(map: dict = ...):
    if map is ...:
    map = {}
    return map
    # Eş değeri
    def foo(map: dict = None):
    if not map:
    map = {}
    return map
  • * regexi olarak kullanma
    # tuple_example.py
    numbers: tuple[int, ...]
    # Allowed:
    numbers = ()
    numbers = (1,)
    numbers = (4, 5, 6, 99)
    # Not allowed:
    11numbers = (1, "a")
    12numbers = [1, 3]
  • slice işlemi olarak kullanma
    # 3 boyutlu ise, 3 tane parametre olur
    arr[:, :, 0]
    # array([[0, 2],[4, 6]])
    # Boyutu bilinmiyorsa
    arr[..., 0]
    # array([[[[ 0, 2], [ 4, 6]], [[ 8, 10], [12, 14]]], [[[16, 18],[20, 22]],[[24, 26],[28, 30]]]])
2023 © Yunus Emre AK