我想在Python中实现装饰器模式,我想知道是否有一种方法可以编写一个装饰器来实现它想要修改的函数,而无需为所有刚转发到装饰对象的函数编写样板.像这样:
class foo(object):
def f1(self):
print "original f1"
def f2(self):
print "original f2"
class foo_decorator(object):
def __init__(self, decoratee):
self._decoratee = decoratee
def f1(self):
print "decorated f1"
self._decoratee.f1()
def f2(self): # I would like to leave that part out
self._decoratee.f2()
Run Code Online (Sandbox Code Playgroud)
我foo_decorator.f2想要decoratee.f2自动转发电话.有没有办法编写一个泛型方法,将所有未实现的函数调用转发给decoratee?
我想换一个vector<char>与std::istream(所以读完载体将通过完成istream接口)
这样做的方法是什么?
由于未定义的行为,许多不好的事情发生并且继续发生(或者不知道,谁知道,任何事情都可能发生).据我所知,这是为了让编译器进行优化留下一些摆动空间,也可能使C++更容易移植到不同的平台和架构.然而,由未定义的行为引起的问题似乎太大而无法通过这些论证来证明.未定义行为的其他参数是什么?如果没有,为什么还存在未定义的行为?
编辑为我的问题添加一些动机:由于使用较少C++的几个糟糕经历 - 狡猾的同事,我已经习惯了使我的代码尽可能安全.断言每一个论点,严谨的正确性以及类似的东西.我试图离开,因为小房间可能以错误的方式使用我的代码,因为经验表明,如果有漏洞,人们会使用它们,然后他们会打电话给我,说我的代码很糟糕.我认为让我的代码尽可能安全是一种好的做法.这就是为什么我不明白为什么存在未定义的行为.有人可以给我一个未定义行为的例子,这些行为在运行时或编译时无法检测到而没有相当大的开销吗?
#include <boost/thread/thread.hpp>
#include <iostream>
void hello()
{
std::cout <<
"Hello world, I'm a thread!"
<< std::endl;
}
int main(int argc, char* argv[])
{
boost::thread thrd(&hello);
thrd.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图编译这个程序,并得到这些错误:
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to
`boost::thread_resource_error::thread_resource_error()'
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to
`boost::thread_resource_error::~thread_resource_error()'
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to
`typeinfo for boost::thread_resource_error'
./src/thread.o: In function `condition_variable':
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33:
undefined reference to `boost::thread_resource_error::thread_resource_error()'
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33:
undefined reference to `boost::thread_resource_error::~thread_resource_error()'
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33: \
undefined reference to `typeinfo for boost::thread_resource_error'
./src/thread.o: In function `thread_data_base':
/usr/include/boost/thread/pthread/thread_data.hpp:54:
undefined reference to `vtable for boost::detail::thread_data_base' …Run Code Online (Sandbox Code Playgroud) 我观察到运行以下java代码的错误行为:
public class Prototype {
public static void main(String[] args) {
final int start = Integer.MAX_VALUE/2;
final int end = Integer.MAX_VALUE;
{
long b = 0;
for (int i = start; i < end; i++) {
b++;
}
System.out.println(b);
}
{
long b = 0;
for (int i = start; i < end; i++) {
b++;
}
System.out.println(b);
}
}
}
Run Code Online (Sandbox Code Playgroud)
两个循环完全相同.然而,第二个输出非确定性的错误值.我正在使用Version在Linux上运行代码:
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode) …Run Code Online (Sandbox Code Playgroud) 在对内置类型进行子类化时,我注意到内置类型的方法的返回类型中Python 2和Python 3之间存在相当重要的区别.以下代码说明了这些集:
class MySet(set):
pass
s1 = MySet([1, 2, 3, 4, 5])
s2 = MySet([1, 2, 3, 6, 7])
print(type(s1.union(s2)))
print(type(s1.intersection(s2)))
print(type(s1.difference(s2)))
Run Code Online (Sandbox Code Playgroud)
使用Python 2,所有返回值都是类型MySet.使用Python 3,返回类型是set.我找不到任何关于结果应该是什么的文档,也没有关于Python 3中的更改的任何文档.
无论如何,我真正关心的是:在Python 3中是否有一种简单的方法可以获得Python 2中的行为,而无需重新定义内置类型的每一种方法?
说我有namedtuple这样的:
FooTuple = namedtuple("FooTuple", "item1, item2")
Run Code Online (Sandbox Code Playgroud)
我希望以下函数用于散列:
foo_hash(self):
return hash(self.item1) * (self.item2)
Run Code Online (Sandbox Code Playgroud)
我想要这个,因为我想要item1和item2无关的顺序(我将对比较运算符做同样的事情).我想到了两种方法来做到这一点.第一个是:
FooTuple.__hash__ = foo_hash
Run Code Online (Sandbox Code Playgroud)
这有效,但感觉被黑了.所以我尝试了子类化FooTuple:
class EnhancedFooTuple(FooTuple):
def __init__(self, item1, item2):
FooTuple.__init__(self, item1, item2)
# custom hash function here
Run Code Online (Sandbox Code Playgroud)
但后来我明白了:
DeprecationWarning: object.__init__() takes no parameters
Run Code Online (Sandbox Code Playgroud)
那么,我该怎么办?或者这是一个坏主意,我应该从头开始编写我自己的课程?
确切的情况是下一个:我在系统API定义结构CGPoint和CGSize,我希望能写my_point = my_size.我无法修改CGPointstruct,只能编写外部运算符.我可以编写二元运算符(+,-...),但operator=必须在struct内部声明.那还有其他解决方案吗?
我在我的代码中进行的计算中遇到了这个问题,如果divident也为0,则除数为0.在我的代码中,我返回0表示该情况.我想知道,虽然除零通常是未定义的,为什么不对这种情况作出例外?我理解为什么除零是不确定的基本上是它无法逆转.但是,在0/0的情况下我没有看到这个问题.
编辑好了,所以这个问题产生了很多讨论.我犯了一个错误,就是因为它得到了很多选票而过于急切地接受了答案.我现在接受了AakashM的答案,因为它提供了如何分析问题的想法.
我有一个包含环境变量的字符串,例如
my_path = '$HOME/dir/dir2'
Run Code Online (Sandbox Code Playgroud)
我想解析字符串,查找变量并将其替换为字符串:
print "HOME =",os.environ['HOME']
my_expanded_path = parse_string(my_path)
print "PATH =", my_expanded_path
Run Code Online (Sandbox Code Playgroud)
所以我应该看到输出:
HOME = /home/user1
PATH = /home/user1/dir/dir2
Run Code Online (Sandbox Code Playgroud)
在Python中有一种优雅的方式吗?
谢谢!
康纳尔
c++ ×4
python ×4
boost ×1
boost-thread ×1
casting ×1
division ×1
for-loop ×1
inheritance ×1
java ×1
math ×1
overriding ×1
posix ×1
python-3.x ×1
subclass ×1
tuples ×1