Challenge: Shadow lots of built-ins

Chris Angelico rosuav at gmail.com
Sun Apr 24 13:09:49 EDT 2016


This is mostly just for the fun of it, but every now and then I have a
discussion with people about why it's legal to shadow Python's
built-in names, and it'd be handy to have a go-to piece of demo code.
So here's the challenge: Write a short, readable block of code that
shadows as many built-ins as possible.

The rules:

1) The code has to be readable on its own. Doesn't have to be fully
functional (it's okay to presume the existence of a back-end database,
for instance), but a human should be able to parse it easily.
2) PEP 8, please, for consistency.
3) Code should be Python 3.x compatible.
4) Every shadowed name MUST make sense. You would have to plausibly
use this exact same name in some other language.
5) Have fun! Enjoy writing suboptimal code! :)

Here's a starter.

def zip_all(root):
    """Compress a directory, skipping dotfiles

    Returns the created zip file and a list of stuff
    that got dropped into the bin.
    """
    bin = []
    with zipfile.ZipFile("temp.zip", "w") as zip:
        for root, dirs, files in os.walk("."):
            for dir in dirs:
                if dir.startswith("."):
                    dirs.remove(dir)
                    bin.append(os.path.join(root, dir))
            for file in files:
                if not file.startswith("."):
                    zip.write(os.path.join(root, file))
    return zip, bin

That's only four, and I know you folks can do way better than that!

ChrisA



More information about the Python-list mailing list