Every Python dev falls for this (name mangling)

Every Python dev falls for this (name mangling)

mCoding

1 год назад

136,624 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Joseph Mazor
Joseph Mazor - 08.11.2023 04:40

This video seems like you rage quite a project and just had to vent in the most productive way possible

Ответить
Yuppi
Yuppi - 20.10.2023 21:57

Why did the python's designers decide to choose the name mangling rules they set? I'm not smart enough to see what benefits they have and what problems they remove. I never liked the double underscore in python aesthetically, it looked like someone just tried to mess with the readability and confuse the observer. Same with selfs everywhere. I might be too dull to use python, but the biggest difficulty for me was that despite it supposedly being more like telling the computer in human language what you want to do, it caused me a lot of difficulties because I didn't understand the rules of how it works and should be used when it looked like it didn't care for rules too much. Like when I tried to speak to it, it actually didn't understand what I wanted. And it still required me to put this thing here and that there, but it just looked like it was careless. The strictness of c++ brought comfort in "these are all the hints that what you're seeing is this and what you're doing is this. you better put the ; there and {} there and painstakingly defining and syntaxing things and explaining the computer step by step what you want to happen".

Ответить
Emma Fountain
Emma Fountain - 20.10.2023 12:31

I love python, but I can’t shake the feeling that it tends to be the wrong choice for any kind of large project. It just doesn’t have the same scope of tools to manage complexity when compared to languages like Java and c sharp.

Ответить
Victor-Marius Pîrvan
Victor-Marius Pîrvan - 06.10.2023 16:02

I've seen some JavaScript code that was using IIFEs to create an exported object containing any functions and classes you would like to expose. And for some object attributes it was mangling them with a random string created when the IIFE was running.

Ответить
Resunoon
Resunoon - 21.09.2023 02:12

oh my god I cant

Ответить
Jona Christopher Sahnwaldt
Jona Christopher Sahnwaldt - 26.08.2023 18:51

Name mangling is fine. The rules aren't complicated. They can be presented in a much simpler and clearer way than in this video. Nothing confusing about them.[1]

Name mangling is useful. Let's say I write an open-source library, and thousands of people use it and extend some of its classes. Without name mangling, I could never safely add a private property to one of these classes, because there may be sub-classes out there that already use the name of my new property. With name mangling, I can safely add any private property I want.

[1] Well, very little is confusing. Of course, mangled module names don't make sense, but that's a purely theoretical problem.

Ответить
Into The Wind
Into The Wind - 21.08.2023 08:59

The most common place to enounter this was when learning to use the `threading` module. It used "dunder" properties for its internal storage in python2, and I think early python 3 (before 3.5).

That said, a single underscore is more akin to "protected" from languages with explicit access controls. It means something isn't part of the public API, but accessing (or even writing to) it may be fine if you are subclassing to extend its functionality.

Double underscore is private , same way that private stuff is in C++. Yes, you can redefine the struct and reinterpret cast in order to access those private fields, but the compiler has gone out of its way to encourage you to not .

Of course, this is largely no longer needed, as modern IDEs will warn you if you are overriding something from higher up the mro. I got my start about 20 years ago, using bare `nano` ; having dunder variables was fairly common, and we were glad to know not to mess with them. I do still use them fairly regularly though, for things I don't want messed with. Because it's done at compile time, there's no performance impact, and I really don't care if someone gets confused when `getattr(foo, "__bar")` doesn't do what they expect. If it breaks when you mess with non-public APIs, you get to keep the pieces.

Ответить
Ilia Salaur
Ilia Salaur - 05.08.2023 13:43

Btw, in C++ we have a best practice to name different class members and methods in a specific way:

* When declaring private method:
_ + myPrivateMethod()

*When declaring a private member (variable): m_ + myMemberName

*When declaring a public member:
think twice, and declare the member itself as private and provide an API to access it (getter and setter)

Ответить
Dr Gamma D
Dr Gamma D - 03.08.2023 06:06

use properties for getters and setters:

class A:
__count = 42
@property
def count(self):
return self.__count

@count.setter
def count(self, value):
self.__count = value


Now if you repeat that code in sub class:

class B(A):

__count = -999
@property
def count(self):
[etcetera]

then:
.>>> a, b = A(), B()
>>> a.count, b.count
42, -999

each class has a truly private unmangled count read/write public attribute that don't collide.

Ответить
MrVipitis
MrVipitis - 12.07.2023 17:19

I have been learning and using Python for 4+ years. And even teaching an intro class a few times.

And I run into new issues all the time. Right now I struggle with relative imports just because I wanted to be fancy and skip copying code. Two days later I was forced to figure out packaging and refactored a lot of the script I had to now have a module of utils. Feels good, but I still haven't solve my initial issues of reusing a complex function elsewhere. Can't import that anymore because it imports the relative utils package now -.-

Ответить
Alex S
Alex S - 10.07.2023 04:34

My brain is now positively mangled after watching this.

Ответить
k98killer
k98killer - 05.07.2023 16:36

In general, the dynamic features of Python should be avoided out of respect for their power and used very judiciously. There are indeed some scenarios in which they are useful, but doing things the static way is always safer.

Ответить
Norb
Norb - 27.06.2023 18:45

Damn, thank god I don't use underscores in my variables too much

Ответить
BrianDMS
BrianDMS - 24.06.2023 18:10

Damn I knew about this and thought it probably makes sense as a middle ground between true private and no safeguarding at all, but never realized how many rough edges it has...

Ответить
Benj
Benj - 08.06.2023 23:41

This is one of the many reasons why I tend to avoid OOP at all reasonable cost

Ответить
Осипов Никита
Осипов Никита - 08.06.2023 18:31

I think It would be cool use private property instead of singleton pattern

Ответить
Осипов Никита
Осипов Никита - 08.06.2023 18:29

As I used to know in earlier versions of python when you tries to reassign any strictly private property in a class you will get the error!

Ответить
angeld23
angeld23 - 28.05.2023 21:08

what an absolute dumpster fire 😭 let's just all agree to pretend this "feature" doesn't exist

Ответить
Shen Long
Shen Long - 11.05.2023 22:11

I discovered it last year in an article written by Dan Bader, and I didn't even think about someone trying to change the attribute using the "wrong" name.

Ответить
antina
antina - 20.04.2023 17:26

How about writing a custom _getattr__/__setattr_ on your class that would check if its not modified from outside of the owner class to know there is an issue early?

Ответить
Bloom
Bloom - 13.04.2023 13:52

I thought the problem was it being a class field instead of an instance member.. Do you not need a constructor? maybe I don't know enough python

Ответить
AJMansfield
AJMansfield - 27.03.2023 04:30

The main thing name-mangling is legitimately used to avoid name collisions in code that needs to be able to subclass arbitrary types or otherwise inject some sort of extra management attribute into an object of an unknown type.

Ответить
Sevdalink
Sevdalink - 17.03.2023 23:28

One more reason to choose functions over classes whenever possible 😀

Ответить
Lepi Doptera
Lepi Doptera - 20.01.2023 01:38

Trivial setters and getters are a waste of time, to begin with. Replicating the power of the "=" operator is an idiocy that should be punished by the law. Trying to enforce private variables in a dynamic language is also completely braindead, both by the language developers and the language user. OOP and dynamic languages simply don't mix.

Ответить
Alex D
Alex D - 16.01.2023 17:19

You should not have called it 'private name-mangling', because it is just 'name mangling'. Adding 'private' is not only incorrect, but also adds to the confusion that it has anything to do with private variables (which isnt the case).

Ответить
Matthew Croft
Matthew Croft - 12.01.2023 10:04

I want a refund

Ответить
Jonathan
Jonathan - 08.01.2023 03:00

I feel like a deep understanding of this would prevent tens of hours of struggling. But I know I’m going to have to learn this the hard way

Ответить
ExpansiveGymnast10
ExpansiveGymnast10 - 07.01.2023 17:59

Good video!!

Ответить
Poultry Pants
Poultry Pants - 07.01.2023 06:38

Thank you for explaining something that makes -0 sense.

Ответить
Barsay
Barsay - 06.01.2023 17:31

"python dev" is a joke in itself

Ответить
TechSY730
TechSY730 - 04.01.2023 17:01

I think the lesson to learn here is that inheriting state in a non-controlled type hierarchy is something to try to avoid.

This applies to OOP languages in general, not just Python.
With Python having an extra reason for why, this lack of sane way of namespacing member variables across inheritance.

Ответить
vinny142
vinny142 - 04.01.2023 14:49

My first thought was "why is he using double underscore for a variable name? Where did he pick up that strange habit? And: Dunderscore has meaning in Python, you don't do that unless you know what you're doing, in which case: why is this a problem?

Anyway: All this is just more reason to NEVER EVER access an class' internal variable directly.
The other reasons ofcourse being that manually getting or setting an internal variable bypasses all validation of that value so you're literally creating a bug, and that you use classes specifically because you don't want the outside code to be coupled to your code in any other way than the interface you provide for the class.(And yes ofcourse it can be convenient to just access it directly and yes "we are all adults here", and the "one underscore means don't touch" is a nice gimmick, but you go ahead and deploy that strategy for a few years and tell me how it goes as developers come and go in your company and code gets forgotten about and updated and fixed and mutated. It doesn't go well)

And yes, this means I have never fallen for this problem. Stop clickbaiting your titles.

Ответить
Lukæsch
Lukæsch - 04.01.2023 11:55

Whats confusing in your example is that you use __count as an instance variable but also initialize in as an class variable on A.__count = 0
tldr. the line after class A: should not be there

Ответить
Francis Charbonneau
Francis Charbonneau - 03.01.2023 07:19

Python is the worst.

Ответить
Groaning Mole
Groaning Mole - 03.01.2023 06:14

Before I saw this video: If you want to use private variables, do it in C++, not Python.
After I saw this video: If you want to use classes, do it in C++, not Python.

Ответить
Don Wald
Don Wald - 03.01.2023 03:58

What a POS language.

Ответить
Ozan
Ozan - 01.01.2023 03:59

After working with a strict language, it's incredibly hard to understand how Python works in many cases.

Ответить
Yoav Goldhorn
Yoav Goldhorn - 01.01.2023 02:05

I'm currently studying Python after having learned C++ and Java and I absolutely hate it.
Like, fine, dictionaries and tuples are nice, but shit like mangling and lack of type declaration is just not worth it. And the syntax isn't all that cleaner or simpler in my eyes... Sure it's easier for complete newbies to stare at, but it's not at all more self-explanatory so trying to read someone else's code is harder than Java for example, and if you actually work with code you're probably going to read other people's code far more often than have to explain the code to a non-coder.

Besides python just translates itself into C or Java anyway so why bother, I could just as easily write in Java and be done with it.

Ответить
Nevo Krien
Nevo Krien - 31.12.2022 00:06

If they ever make python 4 i hope to god this gets dumped

Ответить
N G
N G - 30.12.2022 23:21

You really shouldn't be mixing ststic class variables and instanced variables like that anyways. Assign the variable in the constructor

Ответить
kee
kee - 30.12.2022 19:32

Glad I didn't use __ ever. To me it strengthens me in thinking Python is crap. Everything they don't have and other languages have, they try to do something for it, and it ends up in a mess.

Ответить
Niko L
Niko L - 26.12.2022 14:45

python is held together with ducttape...

Ответить
Jarrod
Jarrod - 19.12.2022 17:35

...so just don't use Python. Got it

Ответить
Mario Nascimento
Mario Nascimento - 19.12.2022 13:46

To be fair in other languages nefarious authors can still access private fields via reflection (java), memory transmutation (rust), etc.

Ответить
Pablo Eco Beer
Pablo Eco Beer - 18.12.2022 17:22

Python is a piece of crap. All those _init__, __get, __dict_ and others are awful. Spaces matter -> awful, good luck with merging and refactoring bugs.

Ответить
Yifei Ren
Yifei Ren - 17.12.2022 14:57

You can use single _ and @properpty for you getter, @varname.setter for setter.

Ответить