repr(...) doesn't work the way you expect (intermediate - advanced) anthony explains

แชร์
ฝัง

ความคิดเห็น • 13

  • @AceofSpades5757
    @AceofSpades5757 2 ปีที่แล้ว +3

    But wait... That's exactly how I would expect it to work...

  • @theeFaris
    @theeFaris 2 ปีที่แล้ว +4

    I could watch this all day

  • @SO-dl2pv
    @SO-dl2pv 2 ปีที่แล้ว +3

    Instead of using a lambda function, you could use (can't render correctly on a youtube comment):
    "type(exit).__repr__ = type(exit).__call__"

  • @alexeyzhurov7747
    @alexeyzhurov7747 2 ปีที่แล้ว

    I didn't need this information ... before I watched this video

  • @erwan5112
    @erwan5112 2 ปีที่แล้ว +3

    I once spent a lot of time trying to implement lazy loading by modifying a class __getattribute__ so that it would initialize the instance and then set the instance __getattribute__ to object.__getattribute__ ... Obviously it did not work, as instance.__getattribute__ is never called, I wish I had known the behavior before, it would have prevented me from loosing a lot of time 😄

    • @anthonywritescode
      @anthonywritescode  2 ปีที่แล้ว +2

      yeah making proxy objects is pretty tricky

  • @con-f-use
    @con-f-use 2 ปีที่แล้ว

    Any reason why you did `lambda: exit()` instead of just `exit` on the left hand side of `type(exit).__repr__ = ...`?

    • @anthonywritescode
      @anthonywritescode  2 ปีที่แล้ว

      it's `type(exit).__repr__ = lambda self: exit()` -- need to have a parameter for `self`

    • @con-f-use
      @con-f-use 2 ปีที่แล้ว

      @@anthonywritescode ah right, it would work without the lambda because exit does take an argument, but what is passed as self would be taken as an exit-code. Thanks!