2

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?

Lou
  • 2,200
  • 2
  • 33
  • 66

3 Answers3

1

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]))
Leo Arad
  • 4,452
  • 2
  • 6
  • 17
1

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()
leopardxpreload
  • 767
  • 5
  • 17
0

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 = "")
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40
  • Wouldn't that print the empty spaces, though? E.g. if I have `t` set to `Task 1`, `date` set to nothing and `priority` set to 1, wouldn't it print something like: `Task 1 1` rather than `Task 1 1` – Lou Jun 17 '20 at 10:26
  • Yes, without the spaces, the output seems a bit awkward – Roshin Raphel Jun 17 '20 at 10:27