this post was submitted on 01 Feb 2024
23 points (100.0% liked)
Python
6378 readers
92 users here now
Welcome to the Python community on the programming.dev Lemmy instance!
📅 Events
Past
November 2023
- PyCon Ireland 2023, 11-12th
- PyData Tel Aviv 2023 14th
October 2023
- PyConES Canarias 2023, 6-8th
- DjangoCon US 2023, 16-20th (!django 💬)
July 2023
- PyDelhi Meetup, 2nd
- PyCon Israel, 4-5th
- DFW Pythoneers, 6th
- Django Girls Abraka, 6-7th
- SciPy 2023 10-16th, Austin
- IndyPy, 11th
- Leipzig Python User Group, 11th
- Austin Python, 12th
- EuroPython 2023, 17-23rd
- Austin Python: Evening of Coding, 18th
- PyHEP.dev 2023 - "Python in HEP" Developer's Workshop, 25th
August 2023
- PyLadies Dublin, 15th
- EuroSciPy 2023, 14-18th
September 2023
- PyData Amsterdam, 14-16th
- PyCon UK, 22nd - 25th
🐍 Python project:
- Python
- Documentation
- News & Blog
- Python Planet blog aggregator
💓 Python Community:
- #python IRC for general questions
- #python-dev IRC for CPython developers
- PySlackers Slack channel
- Python Discord server
- Python Weekly newsletters
- Mailing lists
- Forum
✨ Python Ecosystem:
🌌 Fediverse
Communities
- #python on Mastodon
- c/django on programming.dev
- c/pythorhead on lemmy.dbzer0.com
Projects
- Pythörhead: a Python library for interacting with Lemmy
- Plemmy: a Python package for accessing the Lemmy API
- pylemmy pylemmy enables simple access to Lemmy's API with Python
- mastodon.py, a Python wrapper for the Mastodon API
Feeds
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I’m rusty on my type hints because I’ve been living in lua land lately, but from ye olde PEP 20
I’d combine them so the hint was something like
Union[Collection[str], str]
But what if you actually don't want
str
to be valid?If you're writing code that generic, why wouldn't you want
str
to be passed in? For example,Counter('hello')
is perfectly valid and useful. OTOH,average_length('hello')
would always be1
and not be useful. OTOOH, maybe there's a valid reason for someone to do that. If I've got a list of items of various types and want to find the highest average length, I'd want to domax(map(average_length, items))
and not have that blow up just because there's a string in there that I know will have an average length of1
.So this all depends on the specifics of the function you're writing at the time. If you're really sure that someone shouldn't be passing in a
str
, I'd probably raise aValueError
or a warning, but only if you're really sure. For the most part, I'd just use appropriate type hints and embrace the phrase "we're all consenting adults here".Maybe something like passing in a list of patterns which should match some data, or a list of files/urls to download would be examples of where I would like to be generic, but taking in a string would be bad.
But the real solution be to convert it to
foo(*args: str)
. But maybe if you take 2Container[str]
as input so you can't use*args
. But no real world example comes to mind.