mardi 18 avril 2017

check functions in python classes

I want to create a check on inputs of a particular class, I have the following made up example:

class NmbPair:
    def __init__(self, a = None, b = None):
        self.a = a
        self.b = b

    def __eq__(self, other):
        if self.a == other.a and self.b == other.b:
            return True
        return False

class NmbOperation:
    def __init__(self, *, NmbPair1, NmbPair2):
        if not self.check(NmbPair1, NmbPair2): ## this is the check
        self.NmbPair1 = NmbPair1
        self.NmbPair2 = NmbPair2

    def check(self, a, b):
        if a == b:
            return False

    def _add_first_nmb(self):
        self.sum_a = self.NmbPair1.a + self.NmbPair2.a

so i want to check that the input NmbPairs are not the same, and if they are I do not want an instance of NmbOperation to be created. But this throws the error:

AttributeError: 'NmbOperation' object has no attribute 'sum_a'

I'm not quite sure what I'm doing wrong


0 commentaires:

Enregistrer un commentaire