Skip to content
HomeVideosBooksBlogs
AllTop postsTopics
Podcasts
HistoryListsWatch laterLiked videosFollowing

The videos, books, blogs and podcasts Hacker News keeps recommending, all in one place.

VideosBooksBlogsPodcastsAboutPrivacySettings

Data from Hacker News, HackerTube, HackerBooks, HackerBlogs and Open Library. Credits · Not affiliated with Y Combinator.

HomeVideosBooksBlogsPodcasts
Ctrl K
    R

    Rafe Kettler

    rafekettler.com · Personal blog · on HN since 2011

    Rafe Kettler, a developer, wrote a guide to Python's magic methods and posts on design and the LulzSec password leak.

    Visit blogHN submissions
    ProgrammingLanguages & Compilers
    What HN says

    HN links it for the magic methods guide, cited as a reference, though one commenter says some of its patterns are incomplete or frowned upon.

    Stats for nerds
    16HN people shared it
    7linked in comments
    402story points
    May 2016last shared
    2012people per year2016

    Top posts on HN

    1. 1
      A Guide to Python's Magic Methods
      15 people · 399 points · first shared 2011 · 27 comments on HN
    2. 2
      Design for developers
      1 person · 3 points · first shared 2012 · Discussion on HN
    3. 3
      Analyzing the lulzsec password leak
      1 person · first shared 2013

    What HN people wrote when they linked it

    • scott_sMar 2012

      Honestly, I'm not sure if you can do that in Python - one of the side-effects of not writing classes in Python is that I'm also ignorant of the behind-the-scenes stuff that goes on with them.

      You may be able to get something close to what I wrote with a function that returns a closure, but I don't think you can get exactly the for x in y syntax. As explained in another post on HN's front page, "A Guide to Python's Magic Methods": http://www.rafekettler.com/magicmethods.html#sequence, you need to define the __iter__(self) method on an object to support that style of iteration. (And that method needs to return an iterator object.)

      And that brings us back to the same point I already made, but I think it's so important that I'll say it again: the abstractions you choose to represent concepts in your program will be determined by what is easy in the language you're using.

    • goldenkeyDec 2013

      Methods taking "self" as an argument is a dirty hack, ain't no magic there. The language is just pretty ugly. It's got the whole whitespace thing going on, which is forced, and some might think it gives a good aesthetic but that is dust in the wind in terms of help for Python's ease on the eyes.
      The whole standard namespacing and mix of oop/procedural global functions is just..schizophrenic, the capitalization scheme is dastardly. I haven't seen many languages that look more _basic_ than python. It's a frankenstein of Perl/Ruby and Basic. Forced verbosity is egh.

      Here's your magic methods and properties: http://www.rafekettler.com/magicmethods.html

      Those magic methods kind of remind me of clever hacks while coding in PHP.

    • bruFeb 2014

      Several of those patterns are incomplete or frowned upon:

      * if a method does not use the object's state (no `self` usage) make it a `class-` or `staticmethod`.

      * Some magic methods are presented. There's more to them[0].

      * one should not write `class MyClass:` but `class MyClass(object):` (new style class[1])

      * the last one (`return None`) make me very dubious

      * Cascading methods: that's a big no. The idiom is that if a method may change the state of the object then it should return None (eg `set.add`)

      0: well-written and comprehensive guide: http://www.rafekettler.com/magicmethods.html

      1: http://www.python.org/doc/newstyle/

    • jmountNov 2012

      The ongoing obsession with direct to github store links is irritating. Why not post a link to a designed start of reading point like: http://www.rafekettler.com/magicmethods.html ? Or to https://github.com/RafeKettler/magicmethods/raw/master/magicmethods.pdf so we can share the pleasure of the Scribd cabal screwing up the link and "annoyance wall" the content.

    • orfDec 2015

      More than just built in functions, any object that defines a __len__ and a __index__ method (that provide len(x) and x[0] respectively) can also be iterated over in a for loop ('for item in instance'). A good reference for magic methods is [1] for anyone interested.

      1. http://www.rafekettler.com/magicmethods.html

    • tomkinstinchNov 2014

      I'd love other answers to the same question. Here are a few I've come across in the past:

      http://www.rafekettler.com/magicmethods.html

      http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html

      http://www.siafoo.net/article/52

    Related

    Tom Forbestomforb.es18 people

    Infosys leaked FullAdminAccess AWS keys on PyPI for over a year

    Tom Forbes
    Personal · last shared 2025
    Alex Gaynoralexgaynor.net120 people

    About Python 3

    Alex Gaynor
    Personal · last shared 2026
    PyPy Blogmorepypy.blogspot.com242 people

    Let's Remove the Global Interpreter Lock

    PyPy Blog
    Company · last shared 2024
    Armin Ronacherlucumr.pocoo.org417 people

    996

    Armin Ronacher
    Personal · last shared 2026
    Edward Z. Yangblog.ezyang.com165 people

    PyTorch Internals: Ezyang's Blog

    Edward Z. Yang
    Personal · last shared 2026
    Zed Shawzedshaw.com280 people

    Dear Paul

    Zed Shaw
    Personal · last shared 2026
    Graham Jenkinsdarkcoding.net44 people

    Facebook’s code quality problem (2015)

    Graham Jenkins
    Personal · last shared 2025
    Daniel Miesslerdanielmiessler.com241 people

    It’s Time to Get Back to RSS

    Daniel Miessler
    Personal · last shared 2026