module level __getattr__ (intermediate) anthony explains

Поделиться
HTML-код
  • Опубликовано: 4 июн 2024
  • today I talk about one of the ways to introduce magic in python -- module level `__getattr__`. I also walk through the usual uses of this as well as some of the more magical ones!
    playlist: • anthony explains
    ==========
    twitch: / anthonywritescode
    dicsord: / discord
    twitter: / codewithanthony
    github: github.com/asottile
    stream github: github.com/anthonywritescode
    I won't ask for subscriptions / likes / comments in videos but it really helps the channel. If you have any suggestions or things you'd like to see please comment below!
  • НаукаНаука

Комментарии • 9

  • @Phaust94
    @Phaust94 Год назад +15

    Man, I've been coding Python for 6 years by now - and never heard of __dir__ for tab completion. This channel is pure gold.

  • @Quarky_
    @Quarky_ Год назад

    Thanks for the __dir__ comment, I got stuck on this couple weeks ago when I was implementing __getattr__ to expose attributes of an underlying "private" object.

  • @kai3341
    @kai3341 Год назад

    Thank you for video. Also great thank you for your english -- it's easy to recognize it and then understand

  • @williamli55
    @williamli55 3 месяца назад

    Hello Anthony. I've been watching your videos for a while now, and I think they're great. I was wondering if you might be willing to do a video which goes further into depth on __getattr__, __setattr__, and __getattribute__. I'd love to have a better understanding of how to properly use these methods. I feel like I often don't implement them properly.

  • @dorb1337
    @dorb1337 Год назад

    When I created a def __dir__(self): return list(globals()) under a class C (like you created, just with the __dir__ function inside the class instead of outside to it) and i called c.__dir__() I got a lot of random gibbrish output, do you have any idea what it might point to ? ( My idea was that when globals() is called under a class, it just returns some random memory values that points to stuff, but when printed to the screen has no meaning) . here's my output example - ['__name__',
    '__doc__',
    '__package__',
    '__loader__',
    '__spec__',
    '__builtin__',
    '__builtins__',
    '_ih',
    '_oh',
    '_dh',
    'In',
    'Out',
    'get_ipython',
    'exit',
    'quit',
    '_',
    '__',
    '___',
    '_i',
    '_ii',
    '_iii',
    '_i1',
    'C',
    '_i2',
    '_i3',
    'c',
    '_i4',
    '_4',
    '_i5',
    '_i6',
    '_6',
    '_i7',
    'B',
    '_i8',
    'b',
    '_i9',
    '_9',
    '_i10',
    '_10',
    '_i11',
    'A',
    '_i12',
    '_i13',
    'a',
    '_i14',
    '_i15',
    '_15',
    '_i16',
    '__dir__',
    '_i17',
    '_i18',
    '_18',
    '_i19',
    '_i20',
    '_i21',
    '_21',
    '_i22',
    '_22',
    '_i23']

  • @b_0rk
    @b_0rk Год назад +1

    hi does this also work on python 2.4

    • @cerealport2726
      @cerealport2726 Год назад +1

      You could just try it and see...but as was explained, it seems it was added in 3.7

    • @xan1716
      @xan1716 Год назад +7

      what year is this 😅