简体   繁体   English

如何使python代码更简单

[英]How to make python code simpler

i have following python code.我有以下python代码。 Can i optimise it to one line:我可以将其优化为一行:

url = request.session.get('my_value', '/')
    if url == '/None/':
        url = '/'

Any help will be appericiated任何帮助都会得到帮助

url = re.sub("^/None/$","/",request.session.get('my_value', '/'))

I think this should work:我认为这应该有效:

url = [request.session.get('my_value', '/'), '/']['/None/' in request.session.get('my_value', '/')]

But honestly, it makes the code much more horrible to read...但老实说,这让代码读起来更可怕……

url = '/' if url == '/None/': else url = request.session.get ('my_value', '/')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中使元素检查代码更简单或更简单? - how to make element check code simpler or one-liner in python? 如何在 python 中重复一个动作以使我的代码更简单 - How to repeat an action in python to make my code simpler 如何将 python 循环代码重写为更简单的代码? - How to re-write the python loop code into a simpler code? 如何让我的 pygame konami 代码更简单? - how can I make my pygame konami code simpler? 我怎样才能使这个构造更简单,就像在 python 中使用类或函数一样? - How can I make this construction simpler, like with classes or functions in python? 在 Python 代码中放置 PDB 断点的更简单方法? - Simpler way to put PDB breakpoints in Python code? 我怎样才能使这个 PyTorch 张量(B、C、H、W)平铺和混合代码更简单、更高效? - How can I make this PyTorch tensor (B, C, H, W) tiling & blending code simpler and more efficient? 如何更轻松、更正确地向索引添加附加条件以使代码更简单和覆盖? - How to more easily and correctly add an additional condition to index to make code simpler and covered? 如何使我的代码更简单并仍然获得相同的输出? - How can I make my code simpler and still get the same output? 如何使这种加法更简单? - How can I make this addition method simpler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM