5 ms·
I have a Python function that allows both of those: def randomString(alphabet, length): .... randomHex = partialApply(randomString, alphabet="
by TheLoneWolfling 12y ago
I have a Python function that allows both of those:
def randomString(alphabet, length):
....
randomHex = partialApply(randomString, alphabet="0123456789ABCDEF")
randomBase32 = partialApply(randomString, "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")
random10Char = partialApply(randomString, length=10)
random4Char = partialApply(randomString, length=4)
randomHexByte = partialApply(randomString, "0123456789ABCDEF", 2)
Comes in handy occasionally.
- riwsky 12y agoAnd in case anyone else wants such a function, it's available in the standard library as functools.partial: https://docs.python.org/2/library/functools.html https://docs.python.org/2/library/functools.html
- TheLoneWolfling 12y agoEh, I don't like the one in functools. It allows overriding of variables previously defined, which personally causes more problems than it solves. (So `functools.partial(foo, b=3)(a=1, b=2)` doesn't throw an error, and is equivalent to calling `foo(a=1, b=2)`)
- CrossEye 12y agoI think languages that have named parameters have very different sorts of techniques for these things. I don't know if they're more powerful or not, but they take a very different approach, and offer different design choices.