Print only if not null in Python - one-line method?

Viewed 299

I have a class object, Task, with four properties, t, date, priority and checked. Only t must contain a value, the other three properties are optional. I've written a print method which will print the strings if they're not null:

class Task:
    def __init__(self, _t, _date=None, _priority=None, _checked=False):
        self.t = _t
        try:
            self.date = parser.parse(_date, dayfirst=True) if _date else None
        except:
            self.date = None
        self.priority = _priority
        self.checked = _checked

    def print(self):
        print(self.t, end="")
        if self.date:
            print(self.date, end="")
        if self.priority:
            print(self.priority, end="")

... But I was wondering if there's a way of compressing this into a single line. In VB.NET, you can do something like this:

Console.Writeline(me.t, If(me.date Is Not Nothing, me.date, ""), If(me.priority Is Not Nothing, me.priority, ""))

I tried doing this in Python something like below, but it doesn't work the same way:

print(self.t, if(self.date, self.date), if(self.priority, self.priority))

Is there a one-line solution, or any neater way?

3 Answers

You can use filter with None or bool and by that to avoid printing None values

def print(self):    
    print(*filter(None, [self.t, self.date, self.priority]))

Or

def print(self):    
    print(*filter(bool, [self.t, self.date, self.priority]))

You could create a list and then check if the list is not None:

class Task:
    def __init__(self, _t, _date=None, _priority=None, _checked=False):
        self.t = _t
        try:
            self.date = parser.parse(_date, dayfirst=True) if _date else None
        except:
            self.date = None
        self.priority = _priority
        self.checked = _checked
        self.print_list = [_t, _date, _priority]

    def print(self):
        [print(i, end=' ') for i in self.print_list if i is not None]

t = Task(_t=4, _date=25)
t.print()

A list comprehension for each variable that is not None:

def print(self):
        [print(i, end=' ') for i in self.print_list if i is not None]

Also equivalent:

def print(self): print(*[i for i in self.print_list if i is not None], end="")

Possibly even do the two birds one stone with a dict

class Task:
    def __init__(self, _t, _date=None, _priority=None, _checked=False):
        try:
            temp_date = parser.parse(_date, dayfirst=True) if _date else None
        except:    # I suggest making the except capture a more specific case
            temp_date = None
        self.entities = {'t': _t, 'date': temp_date, 'priority': _priority}

    def print(self): print([v for _, v in self.entities.items() if v is not None], end="")

t = Task(_t=4, _date=25)
t.print()

You can concatenate them and print, this will take care of the nulls:

def print(self):
    string= self.t + " " + self.date + " " + self.priority
    print(string,end = "")
Related