简体   繁体   English

如何使用 Visual Studio 代码分隔 python 中的代码块?

[英]How to seperate code blocks in python using visual studio code?

I am used to coding in SQL where I can use the ';'我习惯在 SQL 中编码,我可以在其中使用“;” symbol to separate two pieces of code and run them individually on one file.用于分隔两段代码并在一个文件上单独运行它们的符号。 This makes testing specific parts of the code really easy.这使得测试代码的特定部分变得非常容易。 However, in python, I haven't found a way of testing bits of my code without having to create a new file and copy and pasting code.但是,在 python 中,我还没有找到一种无需创建新文件并复制和粘贴代码即可测试代码位的方法。 I am using VS Studio Code.我正在使用 VS Studio 代码。

Kind Regards亲切的问候

Use functions .使用函数

Functions are a good way of encapsulating blocks of code that you want to run repeatedly or in a specific context.函数是封装要重复运行或在特定上下文中运行的代码块的好方法。 For your application, you could do:对于您的应用程序,您可以执行以下操作:

def firstPieceOfCode():
    # do first job when called
    ...

def secondPieceOfCode():
    # do second job when called
    ...

secondPieceOfCode() # only do second job for now

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM