python - 如何只运行文件中的特定代码行?
问题描述
我想知道如何只运行一小部分代码而不运行所有以前的代码,这样的事情对我来说非常方便,因为我编写代码只是为了看看它是如何工作的,我在同一个 Python 中这样做文件,所以当我只想运行我最近编写的文件时,它也会运行所有以前的文件。
我只想运行突出显示的部分,而不是前面的所有行:
解决方案
您应该将所有代码都放在函数中。并在主函数中调用它。例如:
# Import necessary libraries.
# Define your functions from the top
def Foo():
print('foo')
def Bar():
print('bar')
if __name__ == '__main__':
# Run your code from here, call the needed functions.
Foo()
Bar()
# If you don't want to run Bar(), simply comment it.
推荐阅读
- neo4j - 如何将嵌套列表插入具有直接关系的 neo4j(密码查询语言)
- visual-studio-code - 为 VS Code 添加 Intellisense 和自动完成功能
- ios - 如何删除 inputAccessoryView 并将其替换为另一个视图?
- android - 在 Android 的 Firestore 中的集合中计数数组
- asp.net-core - Microsoft.AspNetCore.Authentication.OpenIdConnect 引发“无法将令牌响应正文解析为 JSON”错误
- rest - POST 到 Asp.Net Web Api 中的多个表
- node.js - 如何在 Nextjs 中添加 sitemap.xml
- javascript - 如何突出显示段落中大于或等于 8 个字符的单词?JavaScript
- javascript - 如何创建子元素并将其移动到父元素中?
- c# - 为什么实体框架不初始化字段?