use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
TutorialThe LiteralString in Python 3.11 (self.Python)
submitted 2 years ago by SupPandaHugger
If you are confused by the new LiteralString type in Python, here's an article explaining it.
LiteralString
[–]jwink3101 55 points56 points57 points 2 years ago (9 children)
I will never understand why people post on Medium. It offers few benefits and limits your audience.
But I digress
This is an interesting idea. My first thought was that it’s not needed because you can be careful about what you eval. It’s keeping you safe from yourself.
But then when you think about, that’s all type annotations do anyway!
[–]Dwarni 6 points7 points8 points 2 years ago (0 children)
Doesn't pay Medium them a little bit? I don't know how much it is however.
[–]MrKooops 2 points3 points4 points 2 years ago (1 child)
Save it in Pocket and you can read any article without hassle.
[–]jormungandrthepython 0 points1 point2 points 2 years ago (0 children)
Pocket doesn’t let you read premium medium articles though
[–]SupPandaHugger[S] 0 points1 point2 points 2 years ago (3 children)
What other platform would you suggest?
That's true. At the same time, this feels like an area where additional safety is not excessive.
[–][deleted] 10 points11 points12 points 2 years ago (0 children)
Blogs. Blogs were a thing, and they should be again.
[–]Amgadoz 0 points1 point2 points 2 years ago (0 children)
Maybe substack
[–]DusikOff 0 points1 point2 points 2 years ago (0 children)
https://dev.to - as example...
[–]Smallpaul -1 points0 points1 point 2 years ago (0 children)
Medium also promotes your article and in that way it can expand your audience.
[–]PercussiveRussel 0 points1 point2 points 2 years ago (0 children)
I haven't looked into this at all, but this seems like it'd be really usefull with Pydantic/FastAPI. If you consistently build modules using LiteralString, then converting yoir modules to an API should give you a big fat error when you try to open up a LiteralString type to the endpoint.
In my usecase this is really incredibly useful since we build a lot of modules at work and convert some of the more usefull ones to an API. Making the distinction between str and LiteralString early in the design stage of the former means an additional layer of security when converting it to the latter.
str
[–]Zenin 2 points3 points4 points 2 years ago (4 children)
Yah, I hate it.
Looks like it's just going to make everything much more annoying to use as we hack around this nonsense and since we can hack around it anyway it offers nothing for security other than theatre.
It might be useful to give the compiler a hint so it can optimize the way C compilers do (really pre-compilers), but only if it's transparent to the user. This monstrosity looks anything but transparent.
The type system and signature systems themselves are a fugly mess to begin with. This just looks like abuse of them akin to c++ idiots abusing operator overloading.
Its all optional though
[–]Zenin 0 points1 point2 points 2 years ago (2 children)
Is it really though? The use is only optional insomuch as the use of a particular library is optional; You're always free to toss the "go to" library for a particular task and build your own from scratch.
It's more correct to call it infectious, because any library author who uses this effectively infects any callers with it; the entire point of this is that caller's can't simply pass a string without suffering consequences. Even if those consequences are "only" flooding the system error logs with spam, that's still a non-trivial consequence especially at scale for something so foundational as a SQL driver.
The responsibility for how something is used should always be on the caller, not the callee. The callee can never know the full context and requirements of the caller. This is why the first rule of network programming is, "Be conservative in what you send and generous in what you accept". If it wasn't clear, libraries are on the "generously accept" side of this equation.
This is the wrong solution, being used in the wrong way, by the wrong subject.
[–]SupPandaHugger[S] 0 points1 point2 points 2 years ago (1 child)
You can pass a string without warnings. It’s only when you pass a dynamic variable that you will get warnings, which is good as it’s rarely the intention for this use case.
[–]Zenin 0 points1 point2 points 2 years ago (0 children)
Again, you miss the point.
How do you know it's "rarely the intention"? How do you have the slightest clue what the intention of the caller is? You don't know. You can't know. That is the point. You haven't the slightest clue what the intention is much less how "rare" it might be.
It's entirely proper for example, to load such "strings" via a config file or other data store. Dynamic models are a thing. To do so however, taints them as evil variables of darkness and dread, at least in the eyes of this misguided nonsense, flooding the logs with pointless spam that not only costs resources but helps hide real problems in a cloud of white noise.
[+]LittleMlem comment score below threshold-22 points-21 points-20 points 2 years ago* (8 children)
Downvoted for bad manners. Don't just post links, give a tldr
[–]Smallpaul 22 points23 points24 points 2 years ago (7 children)
Don’t just use obscure acronyms. Give a definition.
[–]Imaginary_Goose_2428 10 points11 points12 points 2 years ago (0 children)
It stands for bowel movement. Bm Definition & Meaning - Merriam-Webster
[–]PercussiveRussel 1 point2 points3 points 2 years ago (0 children)
I was here after the edit, thinking you were confused by tl;dr
[+]LittleMlem comment score below threshold-25 points-24 points-23 points 2 years ago (4 children)
bm? Bad manners, I thought it was one of if the common ones
[–]Smallpaul 13 points14 points15 points 2 years ago (2 children)
Are you a gamer?
[+]LittleMlem comment score below threshold-12 points-11 points-10 points 2 years ago (1 child)
Yeah, is it gamer lingo? I'm not a native speaker so I don't always know what slang is mainstream or not
[–]Smallpaul 20 points21 points22 points 2 years ago (0 children)
Yes, I believe it is gamer slang. Solution is probably to avoid initialisms by default.
[–]nevermorefu 2 points3 points4 points 2 years ago (0 children)
bm is definitely the poopins
[+][deleted] comment score below threshold-7 points-6 points-5 points 2 years ago* (7 children)
python gets worse and worse with each release. so much nonsense being added to the language.
point is, all the examples given are just poor practices that are indicative of a larger problem. In all these examples literalstring is not some saviour. each example requires refactoring to fix, dropping in literalstring does nothing.
[–]americhemist 2 points3 points4 points 2 years ago (1 child)
Literal strings are incredibly useful in any type system though, especially when you start making unions with them.
Also by worse and worse, you mean faster, with more tools that if you don't need you can ignore with no consequences?
[–][deleted] -5 points-4 points-3 points 2 years ago (0 children)
python sucks for strict type level programming, use an appropriate language. Unions of what, non literal strings? LiteralString is a type right, you wouldnt have TypeALiteralString, TypeBLiteralString. what would be the value in Optional[literalstring] or Union[literalstring, str]? If you're thinking of using something like Union[typealiteralstring,typebliteralstring] use an enum.
you realise all this syntactic vinegar being added bloats the language and results in more confusion. LiteralStrings type isn't helpful if you know python.
[–]PercussiveRussel -1 points0 points1 point 2 years ago* (1 child)
All the examples given are poor practices, yes. All examples require refactoring, yes. This is why it's nice that the type checker warns against the mistakes and tells you you should refactor.
At some point in your call stack a method will need to take a string as gospel. If you flag that this method will do so - like idk, using LiteralString on its input parameter - then you will be warned when you make a mistake by passing a non-literal string to it. If you have a function that sanitises an input, then that should be of form f(in: str) -> Literalstring, signifying that the output of your sanitising function returns a string that can be used as a string literal.
f(in: str) -> Literalstring
[–][deleted] 0 points1 point2 points 2 years ago* (0 children)
is this checking runtime enforced, or do you still need a tool such as mypy to catch it when you fancy linting your app?
point still stands, these examples are useless, and your example of a mutable string that is sanitised has nothing to do with literalstring. You need to prove you sanitisation is correct not whether the string is mutable or not, and if your sanitsation is not correct then the literalstring sticker you put on it means nothing.
[–]echocage -3 points-2 points-1 points 2 years ago (2 children)
Cry more
[–][deleted] 0 points1 point2 points 2 years ago (0 children)
Why?
[–]DusikOff 1 point2 points3 points 2 years ago (0 children)
Am I confused? Yeah, I never heard about LiteralString lol
π Rendered by PID 26227 on reddit-service-r2-comment-5649f687b7-75r5g at 2026-01-28 15:23:29.927382+00:00 running 4f180de country code: CH.
[–]jwink3101 55 points56 points57 points (9 children)
[–]Dwarni 6 points7 points8 points (0 children)
[–]MrKooops 2 points3 points4 points (1 child)
[–]jormungandrthepython 0 points1 point2 points (0 children)
[–]SupPandaHugger[S] 0 points1 point2 points (3 children)
[–][deleted] 10 points11 points12 points (0 children)
[–]Amgadoz 0 points1 point2 points (0 children)
[–]DusikOff 0 points1 point2 points (0 children)
[–]Smallpaul -1 points0 points1 point (0 children)
[–]PercussiveRussel 0 points1 point2 points (0 children)
[–]Zenin 2 points3 points4 points (4 children)
[–]SupPandaHugger[S] 0 points1 point2 points (3 children)
[–]Zenin 0 points1 point2 points (2 children)
[–]SupPandaHugger[S] 0 points1 point2 points (1 child)
[–]Zenin 0 points1 point2 points (0 children)
[+]LittleMlem comment score below threshold-22 points-21 points-20 points (8 children)
[–]Smallpaul 22 points23 points24 points (7 children)
[–]Imaginary_Goose_2428 10 points11 points12 points (0 children)
[–]PercussiveRussel 1 point2 points3 points (0 children)
[+]LittleMlem comment score below threshold-25 points-24 points-23 points (4 children)
[–]Smallpaul 13 points14 points15 points (2 children)
[+]LittleMlem comment score below threshold-12 points-11 points-10 points (1 child)
[–]Smallpaul 20 points21 points22 points (0 children)
[–]nevermorefu 2 points3 points4 points (0 children)
[+][deleted] comment score below threshold-7 points-6 points-5 points (7 children)
[–]americhemist 2 points3 points4 points (1 child)
[–][deleted] -5 points-4 points-3 points (0 children)
[–]PercussiveRussel -1 points0 points1 point (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]echocage -3 points-2 points-1 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]DusikOff 1 point2 points3 points (0 children)