👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
In other languages, a general guideline that helps produce better code is always make everything as hidden as possible. If in doubt about whether a variable should be private or protected, it"s better to go with private.
Does the same hold true for Python? Should I use two leading underscores on everything at first, and only make them less hidden (only one underscore) as I need them?
If the convention is to use only one underscore, I"d also like to know the rationale.
Here"s a comment I left on JBernardo"s answer. It explains why I asked this question and also why I"d like to know why Python is different from the other languages:
I come from languages that train you to think everything should be only as public as needed and no more. The reasoning is that this will reduce dependencies and make the code safer to alter. The Python way of doing things in reverse -- starting from public and going towards hidden -- is odd to me.
👻 Read also: what is the best laptop for engineering students?
Python name mangling __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
Python name mangling __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from Python name mangling, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Python name mangling
- Deutsch Python name mangling
- Français Python name mangling
- Español Python name mangling
- Türk Python name mangling
- Русский Python name mangling
- Português Python name mangling
- Polski Python name mangling
- Nederlandse Python name mangling
- 中文 Python name mangling
- 한국어 Python name mangling
- 日本語 Python name mangling
- हिन्दी Python name mangling
San Francisco | 2023-03-24
Simply put and clear. Thank you for sharing. Python name mangling and other issues with exp was always my weak point 😁. Will get back tomorrow with feedback
Prague | 2023-03-24
Thanks for explaining! I was stuck with Python name mangling for some hours, finally got it done 🤗. I am just not quite sure it is the best method
Boston | 2023-03-24
Simply put and clear. Thank you for sharing. Python name mangling and other issues with http Python module was always my weak point 😁. Checked yesterday, it works!