| Deutsch English Français Italiano |
|
<type-variables-20250118163304@ram.dialup.fu-berlin.de> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!npeer.as286.net!npeer-ng0.as286.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Struggling to understand Callable type hinting
Date: 18 Jan 2025 15:38:37 GMT
Organization: Stefan Ram
Lines: 80
Expires: 1 Jan 2026 11:59:58 GMT
Message-ID: <type-variables-20250118163304@ram.dialup.fu-berlin.de>
References: <mailman.87.1737156810.2912.python-list@python.org> <Callable-20250118125426@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de GtVe3VyU5CLjbe+vK02suwM/yO4cBjwMXahfuEfKRMlocn
Cancel-Lock: sha1:6Gp1VlZDHh9Z085G8BwulU3vM2k= sha256:tUo8uXNWCbT3QcKQrCpwVH6FPKKttJV/lWpykD29Ito=
X-Copyright: (C) Copyright 2025 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Bytes: 4407
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.