Trong bài trước bạn đã biết cách dùng lệnh continue và lệnh break trong Python để thay đổi vòng lặp. Trong phần này chúng ta sẽ tiếp tục một lệnh khác, lệnh pass trong Python, dùng như một placeholder (trình giữ chỗ) cho việc thực thi các hàm, vòng lặp,… trong tương lai.

Trong Python, pass là một lệnh trống. Nói nôm na thì lệnh pass không làm gì cả, nó chỉ giữ chỗ cho các hàm, vòng lặp mà bạn đã thêm vào, nhưng chưa dùng đến trong hiện tại mà để “dành cho con cháu” mở rộng trong tương lai.

Vậy lệnh pass với chú thích, bình luận có giống nhau không? Không nhé, trình biên dịch code sẽ bỏ qua comment hoàn toàn, nhưng pass thì không bị bỏ qua.

Cấu trúc của lệnh pass trong Python

pass

Chúng ta thường sử dụng lệnh này như một placeholder. Giả sử, bạn có một vòng lặp, hoặc một hàm, nhưng chưa biết nên xây dựng nó như thế nào, chưa biết nên code sao cho tối ưu và muốn để lại làm sau. Nhưng hàm, lệnh đó không thể có một khối lệnh rỗng, trình biên dịch sẽ báo lỗi, vì thế, chỉ cần sử dụng lệnh pass để xây dựng một khối lệnh rỗng, lúc này trình biên dịch sẽ hiểu và không “phàn nàn” gì bạn nữa.

Ví dụ về lệnh pass

# pass chỉ giữ chỗ cho for
# hàm sẽ được thêm vào sau.
sequence = {'p', 'a', 's', 's'}
for val in sequence:
    pass

Trong đoạn code trên, pass đã tạo nên một khối lệnh rỗng cho vòng lặp for. Khi bạn chạy khối lệnh sẽ thấy chẳng có gì xảy ra cả, với một lệnh pass như thế là thành công rồi đấy.

Với các hàm hay class bạn muốn để dành cũng có thể làm tương tự:

def function(args): pass

hay:

class example: pass

Trong bài tiếp theo bạn sẽ biết về các kỹ thuật lặp trong Python với rất nhiều ví dụ minh họa, đừng bỏ lỡ nhé.

332 BÌNH LUẬN

  1. Hi there, just became aware of your blog through Google, and
    found that it’s truly informative. I am gonna watch out
    for brussels. I will appreciate if you continue this in future.
    Lots of people will be benefited from your writing.

    Cheers!

  2. I really like what you guys are usually up too. Such clever work and coverage!
    Keep up the terrific works guys I’ve incorporated you guys to my personal blogroll.

  3. Wonderful post however , I was wondering if you
    could write a litte more on this topic? I’d
    be very thankful if you could elaborate a little
    bit more. Thank you!

  4. I got this site from my buddy who told me about this web site
    and now this time I am browsing this website and reading very
    informative posts here.

  5. Hey there, I think your blog might be having browser compatibility issues.
    When I look at your blog in Chrome, it looks fine but when opening in Internet Explorer, it has some
    overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!

  6. It’а†s actually a nice and useful piece of info. I’а†m happy that you simply shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.

  7. wonderful post, very informative. I wonder why the opposite specialists of this sector do not understand this. You should proceed your writing. I am sure, you ave a great readers a base already!

  8. Can I just say what a comfort to discover someone who
    really knows what they’re discussing on the net. You certainly realize
    how to bring a problem to light and make it important.
    More and more people should look at this and understand this side of your story.
    I was surprised that you’re not more popular given that you definitely possess the gift.

  9. This very blog is definitely cool and besides factual. I have picked many interesting things out of this source. I ad love to visit it every once in a while. Thanks a lot!

  10. Wow, amazing weblog structure! How lengthy have you been running a blog for? you made running a blog glance easy. The full glance of your web site is great, let alone the content material!

  11. I was suggested this website by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You are wonderful! Thanks!

  12. I will immediately grasp your rss as I can not find your email subscription hyperlink or newsletter service. Do you ave any? Kindly allow me realize in order that I may just subscribe. Thanks.

  13. Useful information. Fortunate me I found your site by accident, and I am shocked why this coincidence did not happened earlier! I bookmarked it.

  14. wonderful issues altogether, you simply won a new reader. What would you recommend in regards to your post that you just made some days ago? Any certain?

  15. Hi there, I found your site via Google whilst searching for
    a similar topic, your web site got here up,
    it appears to be like good. I have bookmarked it in my google bookmarks.

    Hi there, simply turned into aware of your weblog via Google, and located that it’s truly informative.
    I am gonna watch out for brussels. I will be grateful in case you continue this in future.

    A lot of folks might be benefited out of your writing.
    Cheers!

  16. El deporte mejorará su circulación sanguínea y, por tanto, su rendimiento. Estas pastillas pueden ser peligrosas si usted tiene enfermedad cardíaca que requiera que tome nitroglicerina u otros medicamentos que contengan nitratos. La aparición de problemas prostáticos, como el cáncer, se puede detectar y tratar a tiempo, minimizando sus posibles consecuencias, si se toman determinadas precauciones.

  17. Wow! This could be one particular of the most helpful blogs We have ever arrive across on this subject. Actually Excellent. I am also a specialist in this topic so I can understand your effort.

  18. wonderful post, very informative. I wonder why the opposite specialists of this sector do not understand this. You should proceed your writing. I am sure, you ave a great readers a base already!

  19. I was recommended this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You are wonderful! Thanks!

  20. Heya are using WordPress for your site platform? I’m new to the blog world
    but I’m trying to get started and set up my own. Do you require any coding knowledge to make your own blog?
    Any help would be really appreciated!

  21. I think other site proprietors should take this web site as an model, very clean and fantastic user friendly style and design, as well as the content. You are an expert in this topic!

  22. Hi, Neat post. There is an issue together with your website in web explorer,
    could check this? IE still is the marketplace chief and a large
    portion of other people will omit your magnificent writing due to this problem.

  23. I do believe all of the ideas you’ve introduced for your post.

    They’re very convincing and will certainly work. Nonetheless, the posts are too short for beginners.
    May just you please lengthen them a little from next time?
    Thanks for the post.

  24. This is very interesting, You are a very skilled blogger. I ave joined your rss feed and look forward to seeking more of your fantastic post. Also, I ave shared your web site in my social networks!

  25. I think other web site proprietors should take this site as an model, very clean and magnificent user friendly style and design, as well as the content. You are an expert in this topic!

  26. Terrific work! This is the type of info that are supposed to be shared across the net. Shame on Google for not positioning this put up higher! Come on over and talk over with my website. Thank you =)

  27. Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your website is excellent, let alone the content!

  28. This is very interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your wonderful post. Also, I have shared your web site in my social networks!

  29. I think other web site proprietors should take this website as an model, very clean and magnificent user friendly style and design, let alone the content. You are an expert in this topic!

  30. Your style is really unique in comparison to other folks I ave read stuff from. Thanks for posting when you have the opportunity, Guess I will just bookmark this web site.

  31. It as not that I want to duplicate your web-site, but I really like the style and design. Could you let me know which style are you using? Or was it especially designed?

  32. It as in reality a nice and useful piece of info. I am happy that you shared this useful tidbit with us. Please keep us up to date like this. Thanks for sharing.

  33. It as in point of fact a nice and useful piece of information. I am satisfied that you shared this helpful tidbit with us. Please keep us informed like this. Thanks for sharing.

  34. This is really interesting, You certainly are a very qualified blogger. I possess joined your rss and enjoy seeking more of one as fantastic post. Also, I have got shared your blog in my myspace!

  35. I was suggested this website by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are amazing! Thanks!

  36. Usually I do not read article on blogs, but I would like to say that this write-up very pressured me to take a look at and do so! Your writing taste has been surprised me. Thanks, quite great article.

  37. It as not that I want to duplicate your web-site, but I really like the style and design. Could you tell me which theme are you using? Or was it especially designed?

THOÁT KHỎI BÌNH LUẬN

Please enter your comment!
Please enter your name here