Làm thế nào để bạn chọn nhiều dòng trong python?

Chúng ta thường cần bình luận khối mã trong python khi kiểm tra hoặc gỡ lỗi mã. Khi một khối được chuyển thành nhận xét python, nó không đóng góp vào đầu ra của chương trình và giúp xác định chức năng hoặc khối nào đang tạo ra lỗi trong chương trình. Trong bài viết này, chúng ta sẽ xem xét một số phím tắt để nhận xét nhiều dòng mã cùng một lúc trong các IDE python khác nhau. Hãy xem các ví dụ cho từng IDE một

Phím tắt comment nhiều dòng trong Spyder

Trong spyder python IDE, chúng ta có thể nhận xét một dòng mã bằng cách chọn dòng và sau đó sử dụng tổ hợp phím

#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    
5. Thao tác này sẽ biến dòng đơn đã chọn thành nhận xét như hình bên dưới. Hàm được đưa ra trong ví dụ thêm một số và hình vuông của nó vào từ điển python dưới dạng cặp khóa-giá trị

print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    

Sau khi nhấn

#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    
5

#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    

Phím tắt để comment nhiều dòng mã trong spyder IDE trước tiên là chọn tất cả các dòng cần comment rồi nhấn tổ hợp phím ctrl+4. Điều này biến toàn bộ các dòng đã chọn thành một nhận xét python như hình bên dưới


  class MyNumber[]:
      """This is the docstring of this class.
      
      It describes what this class does and all its attributes."""
      def __init__[self, value]:
          self.value=value
      def increment[self]:
          """This is the docstring for this method.
          
          It describes what the method does, what are its calling conventions and
          what are its side effects"""
          self.value=self.value+1
          return self.value
  print [MyNumber.increment.__doc__]

Sau khi nhấn ctrl+4

# =============================================================================
# 
#   class MyNumber[]:
#       """This is the docstring of this class.
#       
#       It describes what this class does and all its attributes."""
#       def __init__[self, value]:
#           self.value=value
#       def increment[self]:
#           """This is the docstring for this method.
#           
#           It describes what the method does, what are its calling conventions and
#           what are its side effects"""
#           self.value=self.value+1
#           return self.value
#   print [MyNumber.increment.__doc__]
# =============================================================================

Chúng tôi cũng có thể sử dụng

#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    
5 để bỏ ghi chú các dòng sau khi chọn chúng khi chúng được ghi chú hoặc ghi chú các dòng sau khi chọn chúng để ghi chú chúng. Trong một số phiên bản của Spyder,
#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    
1 có thể được sử dụng để bỏ ghi chú các dòng mã

Phím tắt comment nhiều dòng trong IDLE

Để nhận xét một khối mã trong IDLE, trước tiên chúng ta phải chọn dòng rồi nhấn tổ hợp phím ctrl+D. Điều này sẽ nhận xét các dòng mã đã chọn như hình bên dưới

 class MyNumber[]:
         """This is the docstring of this class.
         
         It describes what this class does and all its attributes."""
         def __init__[self, value]:
             self.value=value
         def increment[self]:
             """This is the docstring for this method.
             
             It describes what the method does, what are its calling conventions and
             what are its side effects"""
             self.value=self.value+1
             return self.value
     print [MyNumber.increment.__doc__]

Sau khi nhấn

#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    
2

## class MyNumber[]:
##         """This is the docstring of this class.
##         
##         It describes what this class does and all its attributes."""
##         def __init__[self, value]:
##             self.value=value
##         def increment[self]:
##             """This is the docstring for this method.
##             
##             It describes what the method does, what are its calling conventions and
##             what are its side effects"""
##             self.value=self.value+1
##             return self.value
##     print [MyNumber.increment.__doc__]

Để bỏ ghi chú các dòng mã, chúng ta chỉ cần chọn các dòng rồi nhấn

#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    
3. Điều này sẽ bỏ ghi chú các dòng đã chọn

Phím tắt để nhận xét nhiều dòng trong Jupyter Notebook

Chúng ta có thể sử dụng ctrl+/ để nhận xét các dòng mã python đã chọn trong Jupyter Notebook. Điều này biến các dòng mã được chọn thành nhận xét như hình bên dưới


     class MyNumber[]:
         """This is the docstring of this class.
         
         It describes what this class does and all its attributes."""
         def __init__[self, value]:
             self.value=value
         def increment[self]:
             """This is the docstring for this method.
             
             It describes what the method does, what are its calling conventions and
             what are its side effects"""
             self.value=self.value+1
             return self.value
     print [MyNumber.increment.__doc__]

Sau khi nhấn

#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    
4

#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    
1

Để bỏ ghi chú các dòng đã chọn, chúng ta chỉ cần nhấn lại

#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    
4

Nhận xét nhiều dòng trong Pycharm

Nếu chúng tôi phải nhận xét nhiều dòng mã trong Pycharm, chúng tôi có thể chọn các dòng sẽ được nhận xét và sau đó nhấn

#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    
6. Để bỏ ghi chú các dòng, chúng ta chỉ cần chọn các dòng rồi nhấn lại
#print["This line will be commented out."]
def add_square_to_dict[x,mydict]:
    a=x*x
    mydict[str[x]]=a
    return mydict
    
6

Sự kết luận

Trong bài viết này, chúng ta đã thấy các phím tắt để nhận xét nhiều dòng cùng một lúc trong các IDE khác nhau của python như spyder, IDLE, Jupyter Notebook và PyCharm. Hãy theo dõi để biết thêm các bài viết thông tin

Có liên quan

Đào tạo Python được đề xuất

Khóa học. Python 3 cho người mới bắt đầu

Hơn 15 giờ nội dung video với hướng dẫn có hướng dẫn cho người mới bắt đầu. Tìm hiểu cách tạo các ứng dụng trong thế giới thực và nắm vững kiến ​​thức cơ bản

Phím tắt để nhận xét nhiều dòng trong Python là gì?

Từ menu chính, chọn Mã. Nhận xét với Dòng Nhận xét. Nhấn Ctrl+/ .

Chủ Đề