Python Forum

Full Version: whuch is easiest to read?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
which of these is easiest to read?

#0
        if t is str:
            return ''.join([chr(c) for c in o])
        if t is unicode:
            return unicode(''.join([chr(c) for c in o]))
        if t is list:
            return o
        if t is tuple:
            return tuple(o)
        if t is set:
            return set(o)
        if t is bytearray:
            return bytearray(o)
        if t is bytes:
            return bytes(bytearray(o))
#1
        s = ''.join([chr(c) for c in o])
        if t is str:       return s
        if t is unicode:   return unicode(s)
        if t is list:      return o
        if t is tuple:     return tuple(o)
        if t is set:       return set(o)
        a = bytearray(o)
        if t is bytearray: return a
        if t is bytes:     return bytes(a)
#2
        s = ''.join([chr(c) for c in o])
        if t is str: return s
        if t is unicode: return unicode(s)
        if t is list: return o
        if t is tuple: return tuple(o)
        if t is set: return set(o)
        a = bytearray(o)
        if t is bytearray: return a
        if t is bytes: return bytes(a)
#3
        if t is str:
                           return ''.join([chr(c) for c in o])
        if t is unicode:
                           return unicode(''.join([chr(c) for c in o]))
        if t is list:
                           return o
        if t is tuple:
                           return tuple(o)
        if t is set:
                           return set(o)
        if t is bytearray:
                           return bytearray(o)
        if t is bytes:
                           return bytes(bytearray(o))
I suppose it should be taken for granted that t is a type, and not any sort of value, otherwise is won't work that way:
>>> [] is list
False
>>> [3, 4, 2, 4] is list
False
With that supposition, I don't really like any of them. But that's because I like dicts:
>>> options = {
...   str: lambda x, y: x,
...   list: lambda x, y: y,
...   tuple: lambda x, y: tuple(y),
...   set: lambda x, y: set(y),
...   bytearray: lambda x, y: bytearray(y),
...   bytes: lambda x, y: bytes(bytearray(y))
... }
>>> return options[t](s, o)
If you really want to align them, then I'd still do it that way:
>>> options = {
...   str:       lambda x, y: x,
...   list:      lambda x, y: y,
...   tuple:     lambda x, y: tuple(y),
...   set:       lambda x, y: set(y),
...   bytearray: lambda x, y: bytearray(y),
...   bytes:     lambda x, y: bytes(bytearray(y))
... }
yes, t is assigned like t = list at code sections where it is determined that the type it is working with is a list.

because of the limitations, i haven't really liked lambdas in Python. but a dictionary indexed by target type is still doable based on functions made by regular def statements an their names. the limitation i speak of doesn't happen in this. that limitation is that the lambda cannot be defined with statements. in my language creation dabbling i often had things like (if done in a Python syntax form):
base = function digits, number:
    result = ''
    while number:
        result = digits[ number % len( digits ) ] + result
        number //= len( digits )
    return result
in this, the function is created as an anonymous object and reference to it is assigned to the item in that name space with an index of "base". and like can be done in Python, the reference can be further assigned or copied elsewhere and used from there (BTDT). and it is possible to define a bunch of functions as one-liners
    def _str(o,s):       return s
    def _unicode(o,s):   return unicode(s)
    def _bytearray(o,s): return bytearray(o)
    def _bytes(o,s):     return bytes(bytearray(o)
    def _list(o,s):      return o
    def _set(o,s):       return set(o)
    def _tuple(o,s):     return tuple(o)
    d = { str:       _str,
          unicode:   _unicode,
          bytearray: _bytearray,
          bytes:     _bytes,
          list:      _list,
          set:       _set,
          tuple:     _tuple,
}
    return d[t](o,s)
or i can just assign t directly to the functions (defined earlier) then do:
    return t(o,s)

i do have some code near the top of my file like:
    if bytes != str:
        unicode = str
to define that name in Python3.