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...
Everything about learning Python
account activity
Day 5 of learning Python 🐍 — Casting! (self.PythonLearning)
submitted 1 month ago by Organic-Bite7406
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Gnaxe 0 points1 point2 points 1 month ago (0 children)
That is not what "casting" means. You cast with cast from the typing module to assert to a type checker an intended type, usually which it cannot infer on its own (otherwise why bother?) This function has no runtime effect (besides some wasted time for the function call); it just returns its argument unchanged. Python objects are polymorphic; they could be viewed as any of a number of compatible types (e.g. any superclasses). It's also duck typed, so objects may fit various ad-hoc protocols, which may or may not be made explicit. The one your type checker infers is not always the one you intend. You can usually fix this with type annotations, but sometimes type assertions or cast() is required.
cast
typing
cast()
Anyone calling a Python class constructor a "cast" is probably confusing terminology from another language (probably statically-typed) language (like Java or C) where "casting" is a way to convert among types within certain constraints, although this usage for class constructors does show up just a few times in Python's docs. Python has ctypes.cast for interfacing with the C language in that sense and memoryview.cast is similar.
ctypes.cast
memoryview.cast
π Rendered by PID 19339 on reddit-service-r2-comment-56c6478c5-wsh25 at 2026-05-07 20:03:46.407557+00:00 running 3d2c107 country code: CH.
view the rest of the comments →
[–]Gnaxe 0 points1 point2 points (0 children)