From fb833a65920210cf10b4c1623248b91e9ac71e84 Mon Sep 17 00:00:00 2001 From: PinkR1ver <3180102330@zju.edu.cn> Date: Mon, 18 Mar 2024 11:17:15 +0800 Subject: [PATCH] Add note --- .../coding_knowledge/coding_lang_MOC.md | 4 +- .../python/about_private_and_public.md | 17 ++++++ .../python/python_import_different_folder.md | 59 +++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 content/computer_sci/coding_knowledge/python/about_private_and_public.md create mode 100644 content/computer_sci/coding_knowledge/python/python_import_different_folder.md diff --git a/content/computer_sci/coding_knowledge/coding_lang_MOC.md b/content/computer_sci/coding_knowledge/coding_lang_MOC.md index 448f27ef3..e264afcf1 100644 --- a/content/computer_sci/coding_knowledge/coding_lang_MOC.md +++ b/content/computer_sci/coding_knowledge/coding_lang_MOC.md @@ -4,7 +4,7 @@ tags: - basic - coding-language - MOC -date: 2024-03-13 +date: 2024-03-18 --- # Python @@ -15,6 +15,8 @@ date: 2024-03-13 * [Assert in Python](computer_sci/coding_knowledge/python/assert_in_python.md) * [Package in Python](computer_sci/coding_knowledge/python/package_in_python.md) * [Python Namespace Pollution](computer_sci/coding_knowledge/python/python_namespace_pollution.md) +* [About Private and Public, Python as example](computer_sci/coding_knowledge/python/about_private_and_public.md) +* [Python Import for different level folders](computer_sci/coding_knowledge/python/python_import_different_folder.md) # HTML * [HTML Double Curly Braces](computer_sci/coding_knowledge/web/html_double_curly_braces.md) diff --git a/content/computer_sci/coding_knowledge/python/about_private_and_public.md b/content/computer_sci/coding_knowledge/python/about_private_and_public.md new file mode 100644 index 000000000..548647f2f --- /dev/null +++ b/content/computer_sci/coding_knowledge/python/about_private_and_public.md @@ -0,0 +1,17 @@ +--- +title: About Private and Public, Python as Example +tags: + - python + - coding-language +date: 2024-03-18 +--- + +# A good answer + +In Python, "privacy" depends on "consenting adults'" levels of agreement - you can't _force_ it. A single leading underscore means you're not **supposed** to access it "from the outside" -- **two** leading underscores (w/o trailing underscores) carry the message even more forcefully... but, in the end, it still depends on social convention and consensus: Python's introspection is forceful enough that you can't **handcuff** every other programmer in the world to respect your wishes. + +((Btw, though it's a closely held secret, much the same holds for C++: with most compilers, a simple `#define private public` line before `#include`ing your `.h` file is all it takes for wily coders to make hash of your "privacy"...!-)) + +# Reference + +* https://stackoverflow.com/questions/1547145/defining-private-module-functions-in-python \ No newline at end of file diff --git a/content/computer_sci/coding_knowledge/python/python_import_different_folder.md b/content/computer_sci/coding_knowledge/python/python_import_different_folder.md new file mode 100644 index 000000000..897663f88 --- /dev/null +++ b/content/computer_sci/coding_knowledge/python/python_import_different_folder.md @@ -0,0 +1,59 @@ +--- +title: Python import 异级目录 +tags: + - python +date: 2024-03-18 +--- + +# Learn by Example + +## 主程序和模块程序在同一目录下 + +```python +''' +-- src + |-- mod1.py + |-- main.py +''' + +import mod1 +from mod1 import * +``` + +## 模块程序在父辈目录 + +```python + ''' + -- src + |-- mod1 + |-- __init__.py + |-- mod1.py + |-- main.py + ''' + +import mod1.mod1 +from mod1 import mod1 +``` + + +## 其他目录下的模块 + +```python +''' +-- src + |-- mod1 + |-- __init__.py + |-- mod1.py + |-- sub + |-- main.py +''' + +import sys +sys.path.append("..") +import mod1.mod1 +``` + + +# Reference + +* https://blog.csdn.net/vitaminc4/article/details/78134508 \ No newline at end of file