Python对象中的类属性自己“擦除”

时间:2019-04-24 06:04:44

标签: python-3.x oop printing attributes

我正在使用一些旧的Python2代码,这些代码来自追溯力(http://www.dpacontest.org/home/index.html)的差分功率分析(DPA)竞赛。我正在修改的代码可以在这里找到(https://svn.comelec.enst.fr/dpacontest/code/reference/

令我发疯的当前问题: 本质上,这段代码中的某些python对象会“擦除”自身或使用其默认值,我不知道为什么。我在逻辑流程中找不到能做到这一点的任何代码,而且我也不认为这是范围问题。

我尝试重写部分代码以使用numpy而不是映射lambda函数。我还尝试了无数种不同的代码顺序,并将方法从其类中拉出,并尝试在本地/内联中运行它们。

main.py:

loc_subkeys = brk.get_subkeys()

des_breaker.py

def get_subkeys(self):
    """
    Returns a vector of currently best sboxes subkeys.
    This is an array of 8 integers.
    """
    sk = np.array([])
    for i in range(8):
        sk = np.append(sk, self.__sbox_breakers[i].get_key())
    return sk

sbox_breaker.py

def get_key(self):
    "Gives the current best key"
    if self.__best_key is None:
        marks = np.array([])
        print("p0: ", len(list(self.__key_estimators[0]._key_estimator__p0)))
        print("p1: ", len(list(self.__key_estimators[0]._key_estimator__p1)))
        print("p0: ", len(list(self.__key_estimators[0]._key_estimator__p0)))
        print("p1: ", len(list(self.__key_estimators[0]._key_estimator__p1)))
        for i in range(64):
            ke = self.__key_estimators[i]
            marks = np.append(marks, ke.get_mark())
            self.__best_key = np.argmax(marks)
        return self.__best_key

key_estimator.py-属性

class key_estimator:
    """
    Provides methods to give a mark to the key relatively to the probability f
    the correctness of the key.
    """
    __sbox = None
    __key = None
    __cnt0 = 0    # The accumulated traces count in partition 0
    __cnt1 = 0    # The accumulated traces count in partition 1
    __p0 = None  # The bit=0 estimated partition
    __p1 = None  # The bit=1 estimated partition
    __diff = np.array([])  # The differential trace

sbox_breaker中的打印语句是我的。他们的输出是我现在唯一的提示:

p0:5003(好)

p1:5003(好)

p0:0(???)

p1:0

有什么作用?第二次围绕key_estimator类的属性似乎已经消失了。这不仅发生在p0和p1上,还发生在所有属性上。

该程序的第一个循环有效,但是在第二个迭代(从main开始)上,它失败了,因为属性已被自身擦除。我可以仅通过打印对象的属性来手动“擦除”它们。

1 个答案:

答案 0 :(得分:0)

所以我似乎在解决此问题后已经解决了问题。类属性是由map创建的,它在Python2中返回列表,但在Python3中不返回列表。使用list()将它们放入列表可以解决持久性问题。我无法告诉您为什么打印map属性会导致其清除自身。

相关问题