我遇到了一个让我对 Python 模块感到困惑的问题。我构建了一个最小的工作示例:
\n$ tree\n\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 configuration.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 error_code.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.py\nRun Code Online (Sandbox Code Playgroud)\n应用程序/配置.py
\n$ tree\n\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 configuration.py\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 error_code.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.py\nRun Code Online (Sandbox Code Playgroud)\n应用程序/错误代码.py
\nimport enum\n\nclass ErrorCode(enum.Enum):\n Success = 0\n Error1 = 1\nRun Code Online (Sandbox Code Playgroud)\n测试.py
\n#!/usr/bin/env python\n\nfrom app.configuration import Configuration\nfrom app.error_code import ErrorCode\n\nc = Configuration()\n\nprint(c.status())\nprint(c.status() == ErrorCode.Error1)\nRun Code Online (Sandbox Code Playgroud)\n命令:PYTHONPATH=app ./test.py
输出:
\nfrom error_code import ErrorCode\n\nclass Configuration:\n def status(self):\n return self._status\n\n def __init__(self):\n self._status = ErrorCode.Error1\nRun Code Online (Sandbox Code Playgroud)\n为什么在这种情况下输出显示ErrorCode.Error1为False相等检查?
我想这与模块有关,因为如果在 app/configuration.py …
我需要让C++对象在向量中拥有POSIX文件描述符.我有一个文件路径的向量,我用它来创建我的对象.这是我的代码:
main.cpp中
std::vector<MightyObject> mightyObjects;
std::vector<const char*> paths = {"awesomePath1", "awesomePath2"};
for (std::vector<const char*>::iterator it = paths.begin(); it != paths.end(); ++it)
{
mightyObjects.emplace_back(MightyObject(*it));
}
Run Code Online (Sandbox Code Playgroud)
MightyObject.cpp
MightyObject::MightyObject(const char* path)
{
this->fd = open(path, O_RDWR | O_NONBLOCK);
}
MightyObject::~MightyObject()
{
close(this.fd);
}
MightyObject::MightyObject(const MightyObject& obj)
{
this->fd = dup(obj.fd);
}
MightyObject& MightyObject::operator=(const MightyObject& other)
{
this->fd = dup(other.fd);
return *this;
}
Run Code Online (Sandbox Code Playgroud)
在mightyObjects中,我的对象包含错误的文件描述符......
我究竟做错了什么 ?