我有一个带有更新其对象属性的函数的类。我试图找出哪个更Pythonic:我应该显式返回我正在更新的对象,还是简单地更新对象self?
例如:
class A(object):
def __init__(self):
self.value = 0
def explicit_value_update(self, other_value):
# Expect a lot of computation here - not simply a setter
new_value = other_value * 2
return new_value
def implicit_value_update(self, other_value):
# Expect a lot of computation here - not simply a setter
new_value = other_value * 2
self.value = new_value
# hidden `return None` statement
if __name__ == '__main__':
a = A()
a.value = a.explicit_value_update(2)
a.implicit_value_update(2)
Run Code Online (Sandbox Code Playgroud)
我环顾四周,但没有看到任何明确的答案。
编辑:具体来说,我正在寻找可读性和执行时间。对于任一功能而言,任一类别都会有优势吗?
我正在尝试将MATLAB的刻度定位为与我的网格对齐,但我找不到偏移标签的好方法.
另外,如果我跑set(gca,'XTickLabel',1:10),我的x刻度标签最终从1到5不等.

出于某种原因, logging.basicConfig忽略了我给它的记录器级别.我的脚本中包含以下代码:
# main.py
print logging.getLogger().getEffectiveLevel()
print logger_level
logging.basicConfig(format=logger_format,
level=logger_level,
)
print logging.getLogger().getEffectiveLevel()
Run Code Online (Sandbox Code Playgroud)
输出是:
30
10
30
Run Code Online (Sandbox Code Playgroud)
因此,它实际上并没有设置记录器级别.但是,如果我在交互式解释器中执行此确切代码,我会得到以下输出:
30
10
10
Run Code Online (Sandbox Code Playgroud)
这意味着我正在设置日志记录级别.更令人困惑的是,这是最近发生的事情 - 我不知道我做了什么导致了这种行为.谁能提供一些见解?
编辑:如果遇到相关问题,我已尝试main.py从我的IDE(Sublime Text)和命令行运行,两者都具有相同的结果.我在一个基于conda的虚拟环境中工作,我尝试在环境中运行代码而不是(但只能从解释器 - 脚本无法处理一般环境),结果相同.
我正在尝试创建一个模板类,当我定义一个非成员模板函数时,我得到"没有匹配函数来调用randvec()"错误.
我有一个模板类定义为:
template <class T>
class Vector {
T x, y, z;
public:
//constructors
Vector();
Vector(const T& x, const T& y, const T& z);
Vector(const Vector& u);
//accessors
T getx() const;
T gety() const;
T getz() const;
//mutators
void setx(const T& x);
void sety(const T& y);
void setz(const T& z);
//operations
void operator-();
Vector plus(const Vector& v);
Vector minus(const Vector& v);
Vector cross(const Vector& v);
T dot(const Vector& v);
void times(const T& s);
T length() const;
//Vector<T>& randvec();
//operators
Vector& …Run Code Online (Sandbox Code Playgroud) 我的cout语句有一个非常奇怪的问题.我只用XCode 4试过这个.例如,如果我写,
cout << "this works" << endl;
cout << "this doesnt";
cout << memorySizeLimit << " blocks of memory available." << endl;
Run Code Online (Sandbox Code Playgroud)
我在调试器控制台中看到了所有三个输出语句.但是,如果我将订单更改为,
cout << memorySizeLimit << " blocks of memory available." << endl;
cout << "this works" << endl;
cout << "this doesn't";
Run Code Online (Sandbox Code Playgroud)
我只看到前两个couts.更奇怪的是,如果我改变代码,
cout << memorySizeLimit << " blocks of memory available." << endl;
cout << "this works" << endl;
cout << "this doesn't" << endl;
Run Code Online (Sandbox Code Playgroud)
我看到了所有三个陈述.
当我改变立场时,为什么我不会看到"这不是"cout声明?