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?