Python Forum
whuch is easiest to read?
Poll: which is easiest to read?
You do not have permission to vote in this poll.
#0
0%
0 0%
#1
0%
0 0%
#2
0%
0 0%
#3
0%
0 0%
Total 0 vote(s) 0%
* You voted for this item. [Show Results]

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
whuch is easiest to read?
#1
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))
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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))
... }
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What Is Best/Easiest IDE To Install? abrogard 12 6,333 Aug-14-2018, 09:50 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020