?/100

Every Python dev falls for this (name mangling)

December 5, 2022103824

Description

Don't fall for it! The Zen of Python must not have been invented yet because this feature is confusing, not obvious, implicit, and much more. Private name mangling in Python converts variable usages like __var into _ClassName__var at compile time and there's no warning it's happening until you run into the trap. ā€• mCoding with James Murphy (https://mcoding.io) Source code: https://github.com/mCodingLLC/VideosSampleCode Python docs: https://docs.python.org/3/tutorial/classes.html?highlight=private#private-variables CPython mangle: https://github.com/python/cpython/blob/ab02262cd0385a2fb5eb8a6ee3cedd4b4bb969f3/Python/compile.c#L542 SUPPORT ME ā­ --------------------------------------------------- Sign up on Patreon to get your donor role and early access to videos! https://patreon.com/mCoding Feeling generous but don't have a Patreon? Donate via PayPal! (No sign up needed.) https://www.paypal.com/donate/?hosted_button_id=VJY5SLZ8BJHEE Want to donate crypto? Check out the rest of my supported donations on my website! https://mcoding.io/donate Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, Neel R, Matt R, Johan A, Casey G, Mark M, Mutual Information BE ACTIVE IN MY COMMUNITY šŸ˜„ --------------------------------------------------- Discord: https://discord.gg/Ye9yJtZQuN Github: https://github.com/mCodingLLC/ Reddit: https://www.reddit.com/r/mCoding/ Facebook: https://www.facebook.com/james.mcoding CHAPTERS --------------------------------------------------- 0:00 Unexpected output 2:31 Private name mangling 4:23 Python does not have private variables 4:54 Mangling rules 6:51 Mangling at compile time 8:28 Compiler explorer 10:18 getattr and setattr 10:55 To mangle or not to mangle? 13:53 Thanks

Wheatcha