IT王者荣耀术语全部解释"PG/UT"是什么意思?

I'm often having code written as follows
self.title = item.title().content.string
except AttributeError, e:
self.title = None
Is there a quicker way of dealing with this? a one-liner?
解决方案 What exceptions are you getting from item.title()?
The bare except (horrible practice!) doesn't tell us.
If it's an AttributeError (where item doesn't have a title method, for example),
self.title = getattr(item, 'title', lambda: None)()
might be the one-liner you seek (but performance won't be enormously different,-).
Edit: as the OP entirely changed the question (it was originally just using self.title(), it's now using self.title().content.string, and does specifically catch AttributeError rather than using a bare except), the previous version of this answer of course doesn't apply any more. The proper answer now is: attempting a one-liner is an absurd approach, when the chain of attribute references &c keeps growing longer and longer (how many will there be next time, nine?
Since they jumped from one to three with the first edit...;-).
And with no idea of which of the many elementary operations expressed by that long, -scoffing chain of references might raise the AttributeError, any attempt at optimization would be flying rather blind, too.
本文地址: &
写成我经常有code如下 尝试:
self.title = item.title()。content.string除AttributeError异常,E:
self.title =无 有没有处理这个问题一个更快的方法?一行程序?解决方案 您从得到什么异常item.title()?裸除了(可怕的做法!)没有告诉我们。如果它是一个AttributeError(其中项目不具有标题方法,例如),
self.title = GETATTR(项目,“标题”,拉姆达:无)() 可能是一个班轮你寻求(但性能不会的极大地的不同,你要知道;
- )。 修改:作为OP彻底改变的问题(它最初只是用 self.title(),它现在使用 self.title()。content.string ,并没有专门捕获 AttributeError的,而不是使用裸除外),这个***当然的previous版本不适用了。正确的***是现在:在尝试一个班轮是一个荒谬的做法,属性引用和放大器链时;?不断增加的时间越来越长(多少会不会有下一次,九由于他们从一个跃升到三用?首先编辑...;
- )和与不知道该由长,的pssed的许多基本操作前$ P $的-scoffing引用链可能会提高AttributeError的,在优化任何企图将飞而失明了。
本文地址: &
扫一扫关注官方微信

参考资料

 

随机推荐