在 if 语句中调用函数/使用 print() 调用调用 - 好的做法?

Call Function in if-Statement/ evoke call with print() - good practice?

使用 tinydb 我有一个像这样的数据库操作对象:

#database.py 
class DataBase(object):
    """CRUD access to database."""

    def __init__(self):
        """Initialize database."""
        self.db = TinyDB('/db.json')

    def new(self, **kwargs):
        """Add a new entry to the database."""
        if self.db.insert(kwargs): # 1
            return 'New item added to the database.'
        else:
            return 'Item NOT added to the database.'

来自 tinydb 的方法 'insert' returns 插入后条目的 id,参见#1。所以我使用这个效果来 return 成功/失败消息,当使用 print() 调用该函数时可以显示该消息:

#main.py
#...
@entry.command('new')
@click.argument('first_arg', type=str)
@click.argument('second_arg', type=str)
def entry_new(**kwargs):
    """Create a new entry."""
    if kwargs is not None:
        click.echo(a_db.new(**kwargs)) # 2
#...

问题 #1:

if self.db.insert(kwargs):

是否'good practice'在if块的条件语句中执行insert函数?如果不是,有什么替代方法可以根据 return 值创建一个 if/else 语句?

问题 #2:

click.echo(a_db.new(**kwargs))

将文件插入数据库的整个过程都包含在打印语句中,以便能够访问插入函数的 return 值。 这是 'good practice' 还是有更好的方法来调用插入函数,访问 return 值并打印出来?

提前感谢您的说明!

很难说 'good practice' 是什么,因为人们通常对它们是什么有不同的看法。

1:您没有在其他任何地方使用返回值,因此仅将其包含在条件语句中似乎没问题。如果 insert 方法引发了一些异常,您将不得不处理它,但它似乎并没有处理。

2:同第一个答案,如果你不再使用这个变量,那么就可以了。