App engine - 检查Expando类中是否存在属性

4 投票
2 回答
754 浏览
提问于 2025-04-16 19:30

在一个扩展类(expando class)里,怎么检查某个属性是否有值呢?这是在用Python为App Engine开发的时候常见的问题。

我可以这样做吗:

if Expando_class_name.property_name_to_check:
    do = someStuff

这样做会不会出错呢?

谢谢!

2 个回答

2

更好的方法是使用 dynamic_properties 方法。

if 'foo' in entity.dynamic_properties():  pass
5

使用 hasattr

if hasattr(expando_instance, 'foo'):
  # Do something with expando_instance.foo

撰写回答