Python - 检查字符串是否为空的惯用法,打印默认值

发布于 2024-08-15 11:36:21 字数 170 浏览 2 评论 0原文

我只是想知道,是否有一个Python习惯来检查字符串是否为空,然后如果是则打印默认值?

(上下文是 Django,对于 UserProfile 的 __unicode__(self) 函数 - 基本上,我想打印名字和姓氏(如果存在),然后打印用户名(如果两者都不存在)存在)。

干杯, 胜利者

I'm just wondering, is there a Python idiom to check if a string is empty, and then print a default if it's is?

(The context is Django, for the __unicode__(self) function for UserProfile - basically, I want to print the first name and last name, if it exists, and then the username if they don't both exist).

Cheers,
Victor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

残疾 2024-08-22 11:36:22
displayname = firstname + lastname or username

如果名字和姓氏的空白字符串长度为 0,则有效

displayname = firstname + lastname or username

will work if firstname and last name has 0 length blank string

我们只是彼此的过ke 2024-08-22 11:36:22

我认为这个问题最好在模板中处理,例如:

{{ user.get_full_name|default:user.username }}

它使用 Django 包含的“默认”过滤器。如果您特别关心 None 值,但希望允许空白值(即“”),还有一个“default_if_none”过滤器。 “默认”过滤器将在 None 值和 '' 值上触发。

这是 Django 文档的链接:
http://docs.djangoproject.com/en/dev/ref /templates/builtins/#default

I think this issue is better handled in the templates with something like:

{{ user.get_full_name|default:user.username }}

That uses Django's included "default" filter. There is also a "default_if_none" filter if you are specifically concerned about a None value, but want to allow a blank value (i.e. ''). The "default" filter will trigger on both a None value and a '' value.

Here's the link to the Django docs on it:
http://docs.djangoproject.com/en/dev/ref/templates/builtins/#default

盛夏已如深秋| 2024-08-22 11:36:22

好的,我假设您指的是 __unicode__() 方法。尝试这样的事情(未经测试,但非常接近正确):

from django.utils.encoding import smart_unicode
def __unicode__(self):
    u = self.user
    if u.firstname and u.lastname:
        return u"%s %s" % (u.firstname, u.lastname)
    return smart_unicode(u.username)

我刚刚意识到你要求的是 Python 惯用法,而不是 Django 代码。那好吧。

Ok, I'm assuming you meant __unicode__() method. Try something like this (not tested, but real close to being correct):

from django.utils.encoding import smart_unicode
def __unicode__(self):
    u = self.user
    if u.firstname and u.lastname:
        return u"%s %s" % (u.firstname, u.lastname)
    return smart_unicode(u.username)

I just realized you asked for the Python idiom, not the Django code. Oh well.

您的好友蓝忘机已上羡 2024-08-22 11:36:22

像这样的东西:

name = data.Name or "Default Name"

Something like:

name = data.Name or "Default Name"
红衣飘飘貌似仙 2024-08-22 11:36:22

我的架构将 None 作为未设置的名字或姓氏,因此 Frederico 的答案不起作用。所以:

print ("%s %s" % (firstname, lastname)
       if not (firstname and lastname) 
       else username )

My schema would have None as an unset first- or lastname, so Frederico's answer wouldn't work. So:

print ("%s %s" % (firstname, lastname)
       if not (firstname and lastname) 
       else username )
玩心态 2024-08-22 11:36:21
displayname = firstname+' '+lastname if firstname and lastname else username
displayname = firstname+' '+lastname if firstname and lastname else username
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文