Skip to content
败犬日报 2025-09-28

败犬日报 2025-09-28

1. python 字符串驻留

py
assert id("some_string") == id("some" + "_" + "string")
assert id("some_string") == id("some_string")

有些字符串会被驻留,可以引用同一个字符串对象从而节省内存。驻留的规则取决于实现。

推荐阅读:https://github.com/satwikkansal/wtfpython?tab=readme-ov-file#-strings-can-be-tricky-sometimes

2. std::filesystem::path/,如果 rhs 是绝对路径就会变成 replace

cpp
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;

int main() {
    std::cout << fs::path{"/home/user"} / fs::path{"/etc/passwd"} << std::endl;
    // 输出 "/etc/passwd"
}

python 的 pathlib 也是这样的,可以理解为 / 相当于一个 cd。