1
$\begingroup$
trainung_data = [
    ['Green', 3, 'Apple'],
    ['Yellow', 3, 'Apple'],
    ['Red', 1, 'Grape'],
    ['Red', 1, 'Grape'],
    ['Yellow', 3, 'Lemon'],
]
header = ["color", "diameter", "label"]
def is_numeric(value):
    return isinstance(value, int) or isinstance(value, float)
class Question:   
    def __init__(self, column, value):
        self.column = column
        self.value = value

    def match(self, example):
        val = example[self.column]
        if is_numeric(val):
            return val>=self.value
        else:
             return val>=self.value
    def __repr__(self):
        condition = '=='
        if is_numeric(self.value):
            condition = '>='
        return "Is %s %s %s?" % (header[self.column], condition, str(self.value))

Now if i call the class like:

Question(0, 'Green')

It give me output like this

Is color == Green?
$\endgroup$
0
$\begingroup$

Typing

Question(0,'Green')

should not print out anything. However if you type

print(Question(0,'Green'))

then, the $\_\_$repr$\_\_$ method is called.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.