🇷🇺 Знак подчеркивания в Python / 🇬🇧 Using underscore in Python variables | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

🇷🇺 Знак подчеркивания в Python / 🇬🇧 Using underscore in Python variables

🇷🇺 Хочу понять когда и почему мы ставим перед переменными один или два знака подчеркивания? А как его ещё можно использовать? 🇬🇧 How to use the underscore sign (single or double) with variables in Python? How can this be exploited?

3rd Dec 2017, 9:12 AM
Гончаров Дмитрий
Гончаров Дмитрий - avatar
1 Antwort
+ 13
🇬🇧 Single underscore makes variables weakly private, while double underscore makes them strongly private. Weakly ones are not being imported when "from module_name import *" is called, while strongly private can't be accessed directly (they have to be preceded by the class name). You use both methods for data hiding, which in Python is a bit different than in other languages, since the code is interpreted (thus visible to everyone). In Python it is used mostly for securing the variables or methods from overwriting or overriding when inheritance to subclasses is considered. 🇷🇺 Один знак подчеркивания использовать для создания переменных приватными, то есть более недоступными в рамках кода. Например они не импортируются через "from module_name import *". Два знака подчеркивания - это сильно приватная переменная. Это означает, что её нельзя использовать просто так, надо прежде добавить ещё название класса. Всё это делается для того чтобы хранить переменные от надписания (overwrite/override).
3rd Dec 2017, 10:38 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar