如何在Python中检查属性是否可设置或可删除?
到目前为止我发现的最好的是
type(obj).__dict__["prop_name"].fset is not None
Run Code Online (Sandbox Code Playgroud) 更新2:我不确定为什么这仍然被投票(2014年3月).自从我多年前提出这个问题以来,这似乎是固定的.确保您使用的是最新版本的boost.
更新:可能需要初始化C++流以格式化数字,并且在Python中加载共享库时不会进行初始化?
我在打电话
cout << 1 << "!" << endl;
Run Code Online (Sandbox Code Playgroud)
在通过boost.python导出到共享库的方法中.它不打印任何东西,但如果我这样做
cout << "%" << "!" << endl;
Run Code Online (Sandbox Code Playgroud)
有用.
这很重要因为我想这样做:
ostream& operator <<(ostream &os, const Bernoulli& b) {
ostringstream oss;
oss << b.p() * 100.0 << "%";
return os << oss.str();
}
Run Code Online (Sandbox Code Playgroud)
通过这样做我暴露了:
BOOST_PYTHON_MODULE(libdistributions)
{
class_<Bernoulli>("Bernoulli")
.def(init<>())
.def(init<double>())
.def("p", &Bernoulli::p)
.def("set_p", &Bernoulli::set_p)
.def("not_p", &Bernoulli::not_p)
.def("Entropy", &Bernoulli::Entropy)
.def("KL", &Bernoulli::KL)
.def(self_ns::str(self))
;
}
Run Code Online (Sandbox Code Playgroud)
但是当我str
在伯努利对象上调用python中的方法时,我什么也得不到.我怀疑更简单的cout问题是相关的.
给定索引和大小,是否有更有效的方法来生成标准基础向量:
import numpy as np
np.array([1.0 if i == index else 0.0 for i in range(size)])
Run Code Online (Sandbox Code Playgroud) 什么是存储在Python对象的优点和缺点numpy.array
与dtype='o'
与使用list
(或list
的list
等,在更高的维度)?
在这种情况下,numpy数组是否更有效?(似乎他们无法避免间接,但在多维情况下可能更有效.)
我正在尝试运行以下代码以获得简短的机器学习算法:
import re
import argparse
import csv
from collections import Counter
from sklearn import datasets
import sklearn
from sklearn.datasets import fetch_mldata
dataDict = datasets.fetch_mldata('MNIST Original')
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我试图通过sklearn阅读mldata.org上的数据集"MNIST Original".这导致以下错误(有更多的代码行,但我在此特定行收到错误):
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pydev\pydevd.py", line 1481, in <module>
debugger.run(setup['file'], None, None)
File "C:\Program Files (x86)\JetBrains\PyCharm 2.7.3\helpers\pydev\pydevd.py", line 1124, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "C:/Users/sony/PycharmProjects/Machine_Learning_Homework1/zeroR.py", line 131, in <module>
dataDict = datasets.fetch_mldata('MNIST Original')
File "C:\Anaconda\lib\site-packages\sklearn\datasets\mldata.py", line 157, in fetch_mldata
matlab_dict = io.loadmat(matlab_file, struct_as_record=True)
File …
Run Code Online (Sandbox Code Playgroud) 以下深奥的C++运算符的目的是什么?
成员指针
::*
Run Code Online (Sandbox Code Playgroud)
通过指针将指针绑定到成员
->*
Run Code Online (Sandbox Code Playgroud)
通过引用将指针绑定到成员
.*
Run Code Online (Sandbox Code Playgroud)
(参考)
在C++中,每次函数不修改对象时,在每次函数"符合"const的情况下,在成员函数定义的末尾添加const是一种很好的做法吗?我知道在这种情况下有必要:
class MyClass {
public:
int getData() const;
};
void function(const MyClass &m) { int a = m.getData(); dosomething... }
Run Code Online (Sandbox Code Playgroud)
但除此之外,以及const对实际功能的其他用途,确实在最后添加const实际上改变了代码的执行方式(更快/更慢),或者它只是编译器处理诸如一个案例的'标志'以上?换句话说,如果类中的功能不需要const(最后),添加它会有什么不同吗?
在阅读完这个问题之后,我注意到S. Lott可能喜欢使用"有序的defaultdict",但它并不存在.现在,我想知道:为什么我们在Python中有这么多的dict类?
为什么不这样的,
dict(initializer=[], sorted=False, ordered=False, default=None,
weak_keys=False, weak_values=False)
Run Code Online (Sandbox Code Playgroud)
统一一切,并提供每一个有用的组合?
使用以下示例:
from typing import Callable, Generic, Type, TypeVar
ThetaType = TypeVar('ThetaType', bound=int)
XType = TypeVar('XType', bound=int)
class IteratedFunction(Generic[ThetaType, XType]):
def find_fixed_point(self,
theta: ThetaType,
x_init: XType) -> XType:
return x_init
def combinator(
iterated_function_cls: Type[
IteratedFunction[ThetaType, XType]]) -> Callable[
[IteratedFunction[ThetaType, XType]], XType]:
old_find_fixed_point = iterated_function_cls.find_fixed_point
def new_find_fixed_point(
iterated_function: IteratedFunction[ThetaType, XType],
theta: ThetaType,
x_init: XType) -> XType:
return old_find_fixed_point(iterated_function, theta, x_init)
return new_find_fixed_point
Run Code Online (Sandbox Code Playgroud)
MyPy 说:
a.py:25: error: Incompatible return value type (got "XType", expected "XType")
a.py:25: error: Argument 1 has incompatible type "IteratedFunction[ThetaType, …
Run Code Online (Sandbox Code Playgroud) python ×8
c++ ×3
numpy ×3
boost-python ×1
const ×1
decorator ×1
inheritance ×1
mypy ×1
operators ×1
properties ×1