Friday, April 20, 2018

DISappearing and

Python has a very rich set of operators that can be overloaded.  From __get__ to __getattr__, __repr__ to __format__, and __complex__ to __iadd__ you can modify almost every behavior of your type.  Conspicuously absent however, are the boolean operators.

This is why Django ORM and SQLAlchemy use bitwise & and | to represent SQL and / or.

Let's take a closer look at how the Python compiler treats these operators:

>>> import dis
>>> dis.dis(lambda: a & b)
  1           0 LOAD_GLOBAL              0 (a)
              3 LOAD_GLOBAL              1 (b)
              6 BINARY_AND
              7 RETURN_VALUE
>>> dis.dis(lambda: a and b)
  1           0 LOAD_GLOBAL              0 (a)
              3 JUMP_IF_FALSE_OR_POP     9
              6 LOAD_GLOBAL              1 (b)
        >>    9 RETURN_VALUE

Not only can you not override the and operator, the Python VM doesn't even have an opcode for it.

In return, Python gives you the semantics that a or b returns not True or False, but either or b (or False if neither is truthy).


28 comments:

  1. Great article and a nice way to promote online. I’m satisfied with the information that you provided
    bathroom remodeling nashville tn

    ReplyDelete
  2. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place.
    air conditioning contractors palm beach county

    ReplyDelete
  3. Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading.
    tankless water heater nashville

    ReplyDelete
  4. I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it..
    tree removal companies palm beach county

    ReplyDelete
  5. You have a good point here!I totally agree with what you have said!!Thanks for sharing your views...hope more people will read this article!!!
    air conditioning companies palm beach county

    ReplyDelete
  6. I have read your article, it is very informative and helpful for me.I admire the valuable information you offer in your articles. Thanks for posting it..
    bathtub refinishing nashville

    ReplyDelete
  7. Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject.
    residential pest control wellington fl

    ReplyDelete
  8. Very nice bro, thanks for sharing this with us. Keep up the good work and Thank you for sharing information
    pest control companies royal palm beach fl

    ReplyDelete
  9. I think this is one of the most significant information for me. And i’m glad reading your article. But should remark on some general things, The web site style is perfect, the articles is really great : D. Good job, cheers

    Haroon Ullah

    ReplyDelete
  10. Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subject. Manhattan Escorts

    ReplyDelete
  11. Superior post, maintain up with this remarkable work. It's nice to know that this subject is being additionally covered on this internet site so cheers for taking the time to review this! Thanks again as well as again! Guest Post

    ReplyDelete
  12. If your looking for Online Illinois license plate sticker renewals then you have need to come to the right place.We offer the fastest Illinois license plate sticker renewals in the state. watch series

    ReplyDelete
  13. I simply want to tell you that I am new to weblog and definitely liked this blog site. Very likely I’m going to bookmark your blog . You absolutely have wonderful stories. Cheers for sharing with us your blog. best gaming

    ReplyDelete
  14. I read this article. I think You put a great deal of exertion to make this article. I like your work. real estate closing gifts

    ReplyDelete
  15. The level of interest is high when you know how to start a super mission. I like to give my great line to perform super actions from the top zone. watch series

    ReplyDelete
  16. stunning... what an extraordinary blog, this writter who composed this article it's realy an incredible blogger, this article so moving me to be a superior individual christmas

    ReplyDelete
  17. On the off chance that you are looking for best McAfee specialized help, Our McAfee client care number and locate the best answer for it. social media agency

    ReplyDelete
  18. I can't stand by to burrow profound and the opening shot using assets that I got from you. Your richness is reviving. ecommerce website development company

    ReplyDelete
  19. Great article. On account of this blog my undertaking has really finished. online retailer

    ReplyDelete
  20. Welcome to an amazing gathering here you will get the hang of every little thing about me. gift

    ReplyDelete
  21. I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... Sarkari Naukri

    ReplyDelete
  22. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Amy Coney Barrett

    ReplyDelete
  23. Incredible article and a pleasant method to advance on the web. I'm happy with the data that you gave. judi slot

    ReplyDelete
  24. Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. relationship

    ReplyDelete
  25. Positive site, where did u build up the data on this distributing? I have looked at a couple of the articles on your site presently, just as I really like your plan. An abundance of thanks a million and furthermore please keep up the solid work. high DA

    ReplyDelete
  26. Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. Speedup Forex

    ReplyDelete
  27. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! RRB Group D Admit Card

    ReplyDelete