Coder Social home page Coder Social logo

dunders's Introduction

Using and abusing Python's double-underscore methods and attributes

Anjana Vakil, EuroPython 2016

@AnjanaVakil
vakila.github.io

What's this all about?

The curious Python methods and attributes surrounded by double underscores (__) go by many names, including "special", "magic", and "dunder". You probably use some of them, like __init__, every day. But that’s just the tip of the iceberg!

In this talk, we’ll explore the weird and wonderful world of the double-underscore, and find out how dunders can be useful, silly, dangerous, and just fun! We’ll play pranks on Python’s builtin operators for arithmetic and comparison. We’ll make arbitrary objects behave like dictionaries and containers. We’ll reduce an object’s memory usage, and speed up tests for membership. We’ll even try some naughty function hacks that we should never use in real life!

What are dunders?

They're "special" methods that Python expects to find on certain objects, and calls to perform some basic functionality of the language, such as when using operators like + (calls __add__) or built-in functions like len (calls __len__) . Taking full advantage of the "magic" of Python, and writing Pythonic code, means using these methods properly.

These methods are generally not meant to be exposed to users - they're supposed to be called by the Python interpreter, not us measly humans. But we can call them if we want to, if we know what we're doing - abusing the dunder can be fun! >:D

Where can I read more about dunders?

What's in this repo?

Some sample code that illustrates various dunders and how (not) to use them.

DISCLAIMER: Do NOT write real code that looks like the silly, abusive code in this repo! I accept no responsibility for any dunder-related catastrophes directly or indirectly resulting from this talk. Please dunder responsibly. :)

Contents:

  • stringyint.py: A weird type of number that illustrates:

    • some basic dunders we know & love
    • dunders to use operators like +
    • more curious dunders like __hash__ and __slots__
  • crazylist.py: A loco type of list that illustrates dunders to:

    • give an object length and truthiness
    • make an object iterable
    • make an indexable or keyed object
    • test for membership in a container
  • catification.py: A meowy module that illustrates:

    • hacking a function's code and docstring
    • creating a context manager to be used with with

What should I do next?

Know some dunder tricks you'd like to share? Describe them on the wiki, or add code to sharing-is-caring/ and submit a PR!

Want to discuss something? Open an issue and start the conversation!

I hope this repo will be a "meeting place" for exchanging special dunder magic, even after the talk :)

dunders's People

Contributors

vakila avatar sm6xmm avatar mangeld avatar

Watchers

James Cloos avatar Shaun Patterson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.