小编ada*_*liu的帖子

类方法__instancecheck__不起作用

我在Windows上使用python 2.7.3.我试图将__instancecheck__魔术方法覆盖为类方法.但我不能让它发挥作用.

class Enumeration(int):
    @classmethod
    def __instancecheck__(cls, inst):
        if type(inst) == cls:
            return True
        if isinstance(inst, int) and inst in range(0,10):
            return True
        return False

print isinstance(1, Enumeration)   # prints False
print isinstance(1, Enumeration()) # prints True
Run Code Online (Sandbox Code Playgroud)

我假设第一个print语句将为True.但似乎__instancecheck__没有调用神奇的方法.我不知道为什么第二个print语句可以工作,因为isinstance应该将class/type作为第二个参数.

有谁知道问题是什么?谢谢.

python

13
推荐指数
2
解决办法
6892
查看次数

如何删除目录中从其他目录复制的文件?

我有两个目录,比如A和B.A有几个文件:a1,a2,a3.B还有几个文件:b1,b2.首先,我使用以下ant任务将所有文件从B复制到A.

<copy todir="A" verbose="true">
    <fileset dir="B" includes="*"/>
</copy>
Run Code Online (Sandbox Code Playgroud)

然后我想撤消这些步骤,即删除A中从B复制的文件,即b1和b2.我怎样才能实现这些目标?

注意:示例中的文件名仅用于我们理解问题.我不知道两个目录中的确切文件名.

ant

5
推荐指数
1
解决办法
1612
查看次数

为什么有必要复制volatile变量而不是直接使用它?

实践中的Java并发性一书中,有一个自定义线程的例子(参见8.3.4节中的清单8.7).我粘贴了下面的代码.有一点我不太明白.也就是说,该run()方法debugLifecycle在使用之前复制volatile变量.它有一个注释复制调试标志,以确保始终如一的价值.有没有必要在这里复制变量?如果是,为什么?

public class MyAppThread extends Thread {
    public static final String DEFAULT_NAME = "MyAppThread";
    private static volatile boolean debugLifecycle = false;

    public MyAppThread(Runnable r) {
        this(r, DEFAULT_NAME);
    }

    public MyAppThread(Runnable runnable, String name) {
        super(runnable, name + "-" + created.incrementAndGet());
        setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            public void uncaughtException(Thread t,
                                          Throwable e) {
                log.log(Level.SEVERE,
                        "UNCAUGHT in thread " + t.getName(), e);
            }
        });
    }

    public void run() {
        // Question: why copy the volatile variable …
Run Code Online (Sandbox Code Playgroud)

java synchronization volatile

4
推荐指数
1
解决办法
324
查看次数

标签 统计

ant ×1

java ×1

python ×1

synchronization ×1

volatile ×1