Re: Struggling to understand Callable type hinting

Liste des GroupesRevenir à cl python 
Sujet : Re: Struggling to understand Callable type hinting
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.python
Date : 18. Jan 2025, 16:38:37
Autres entêtes
Organisation : Stefan Ram
Message-ID : <type-variables-20250118163304@ram.dialup.fu-berlin.de>
References : 1 2
ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
T = TypeVar('T')

  Alright, so type variables in Python are like the Swiss Army
  knife of the coding world. They're part of this thing called the
  typing module, and they let you whip up functions and classes
  that can roll with pretty much any type you throw at them.

  Here's the skinny on when you'd need one of these bad boys instead
  of just sticking to one type:

  Python

from typing import TypeVar, List

T = TypeVar('T')

def first_and_last(items: List[T]) -> List[T]:
    return [items[0], items[-1]]

  Check it out - this "first_and_last" function is like a food truck
  that can serve up any cuisine. It takes a list of whatever and
  spits out a list with the first and last items.

  Using this T type variable is like having a menu that changes
  based on what's fresh that day. It keeps everything kosher between
  what goes in and what comes out.

  If we'd used a fixed type, like "List[int]", it'd be like a taco
  truck that only serves carne asada. By using a type variable,
  we're cooking up a function that's as flexible as a yoga
  instructor but still gives the lowdown on types to those picky
  static checkers and other devs. It's the best of both worlds,
  like living in California - you get the beach /and/ the mountains!

  "TypeVar" is a factory for creating type variables. It's not a
  type itself, but a tool to create types.

  When you write "T = TypeVar('T')", you're basically brewing
  your own custom type. It's like creating a signature blend
  at Philz Coffee - unique and tailored to your needs.

  Once you've created this custom type "T", you can use it in
  generic constructions like "List[T]". This tells Python,
  "Hey, I want a list of whatever type T turns out to be".

  The string argument 'T' in TypeVar('T') serves two main purposes:

  1.  It provides a name for the type variable, which is used
      for debugging and error reporting. This name helps
      developers and type checkers identify and track the type
      variable throughout the code.

  2.  It's a convention to use the same string as the variable
      name to which the TypeVar is assigned. This practice
      enhances code readability and maintainability.

  While any string could technically be used, it's strongly
  recommended to follow the convention of using the same
  string as the variable name.

  For example:

  Python

T = TypeVar('T')  # Recommended
AnyStr = TypeVar('AnyStr')  # Also follows the convention

  Using a different string wouldn't break the functionality, but
  it could lead to confusion:

  Python

X = TypeVar('Y')  # Works, but confusing and not recommended

  The name is primarily for human readers and tools. It doesn't
  affect the runtime behavior of the program, but it's crucial
  for static type checking and code comprehension.



Date Sujet#  Auteur
18 Jan 25 * Struggling to understand Callable type hinting6Ian Pilcher
18 Jan 25 +- Re: Struggling to understand Callable type hinting1Paul Rubin
18 Jan 25 `* Re: Struggling to understand Callable type hinting4Stefan Ram
18 Jan 25  +- Re: Struggling to understand Callable type hinting1Stefan Ram
18 Jan 25  +- Re: Struggling to understand Callable type hinting1Stefan Ram
18 Jan 25  `- Re: Struggling to understand Callable type hinting1Stefan Ram

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal