Skip to content

Instantly share code, notes, and snippets.

@rob-smallshire
Created January 9, 2015 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rob-smallshire/8c31b4a302319389803d to your computer and use it in GitHub Desktop.
Save rob-smallshire/8c31b4a302319389803d to your computer and use it in GitHub Desktop.
A Python 3.4 conj function for mutable and immutable sequences and strings.
from functools import singledispatch
from collections import Sequence, MutableSequence
@singledispatch
def conj(sequence, item):
raise TypeError("conj() not supported for type '{}'".format(type(sequence)))
@conj.register(MutableSequence)
def _(sequence, item):
sequence.append(item)
return sequence
@conj.register(Sequence)
def _(sequence, item):
return sequence + type(sequence)((item,))
@conj.register(str)
@conj.register(bytes)
def _(sequence, item):
return sequence + item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment