Skip to main content
Match the variable names
Source Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 327

Add another function to simplify your list comprehension

You can make your code more readable by creating a function to simplify your list comprehension. By defining an additional function like so :

def func_on_str(string, func):
    return any(funfunc(char) for char in sstring)

you can rewrite the return statement of check_string more clearly:

return [func_on_str(string, func) for func in check_funcs]

(you can see I agree with @SuperBiasedMan that your variable names could be clearer)

Use getattr() and the code gets shorter but not necessarily more readable

If you want to run member functions, here's what you can do ignoring SRP...it is nice and short. I think it's readable, but others may disagree.

check_funcs = ["isalnum",
                  "isalpha",
                  "isdigit",
                  "islower",
                  "isupper",
                  ]

for func in check_funcs:
    print(any(getattr(char, func)() for char in string))

That could be your whole main() function if you wrote one. Or if you want to keep a separate function for printing you can do:

result_list = []
for func in check_funcs:
    result_list.append(any(getattr(char, func)() for char in string))
return result_list

or of course you could use a monster list comprehension:

   return [any(getattr(char, func)() for char in string) for func in check_funcs]

But I am not sure any of these versions are more readable than what you've got already.

Add another function to simplify your list comprehension

You can make your code more readable by creating a function to simplify your list comprehension. By defining an additional function like so :

def func_on_str(string, func):
    return any(fun(char) for char in s)

you can rewrite the return statement of check_string more clearly:

return [func_on_str(string, func) for func in check_funcs]

(you can see I agree with @SuperBiasedMan that your variable names could be clearer)

Use getattr() and the code gets shorter but not necessarily more readable

If you want to run member functions, here's what you can do ignoring SRP...it is nice and short. I think it's readable, but others may disagree.

check_funcs = ["isalnum",
                  "isalpha",
                  "isdigit",
                  "islower",
                  "isupper",
                  ]

for func in check_funcs:
    print(any(getattr(char, func)() for char in string))

That could be your whole main() function if you wrote one. Or if you want to keep a separate function for printing you can do:

result_list = []
for func in check_funcs:
    result_list.append(any(getattr(char, func)() for char in string))
return result_list

or of course you could use a monster list comprehension:

   return [any(getattr(char, func)() for char in string) for func in check_funcs]

But I am not sure any of these versions are more readable than what you've got already.

Add another function to simplify your list comprehension

You can make your code more readable by creating a function to simplify your list comprehension. By defining an additional function like so :

def func_on_str(string, func):
    return any(func(char) for char in string)

you can rewrite the return statement of check_string more clearly:

return [func_on_str(string, func) for func in check_funcs]

(you can see I agree with @SuperBiasedMan that your variable names could be clearer)

Use getattr() and the code gets shorter but not necessarily more readable

If you want to run member functions, here's what you can do ignoring SRP...it is nice and short. I think it's readable, but others may disagree.

check_funcs = ["isalnum",
                  "isalpha",
                  "isdigit",
                  "islower",
                  "isupper",
                  ]

for func in check_funcs:
    print(any(getattr(char, func)() for char in string))

That could be your whole main() function if you wrote one. Or if you want to keep a separate function for printing you can do:

result_list = []
for func in check_funcs:
    result_list.append(any(getattr(char, func)() for char in string))
return result_list

or of course you could use a monster list comprehension:

   return [any(getattr(char, func)() for char in string) for func in check_funcs]

But I am not sure any of these versions are more readable than what you've got already.

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Add another function to simplify your list comprehension

You can make your code more readable by creating a function to simplify your list comprehension. By defining an additional function like so :

def func_on_str(string, func):
    return any(fun(char) for char in s)

you can rewrite the return statement of check_string more clearly:

return [func_on_str(string, func) for func in check_funcs]

(you can see I agree with @SuperBiasedMan that your variable names could be clearer)

Use getattr() and the code gets shorter but not necessarily more readable

If you want to run member functions, here's what you can do ignoring SRPSRP...it is nice and short. I think it's readable, but others may disagree.

check_funcs = ["isalnum",
                  "isalpha",
                  "isdigit",
                  "islower",
                  "isupper",
                  ]

for func in check_funcs:
    print(any(getattr(char, func)() for char in string))

That could be your whole main() function if you wrote one. Or if you want to keep a separate function for printing you can do:

result_list = []
for func in check_funcs:
    result_list.append(any(getattr(char, func)() for char in string))
return result_list

or of course you could use a monster list comprehension:

   return [any(getattr(char, func)() for char in string) for func in check_funcs]

But I am not sure any of these versions are more readable than what you've got already.

Add another function to simplify your list comprehension

You can make your code more readable by creating a function to simplify your list comprehension. By defining an additional function like so :

def func_on_str(string, func):
    return any(fun(char) for char in s)

you can rewrite the return statement of check_string more clearly:

return [func_on_str(string, func) for func in check_funcs]

(you can see I agree with @SuperBiasedMan that your variable names could be clearer)

Use getattr() and the code gets shorter but not necessarily more readable

If you want to run member functions, here's what you can do ignoring SRP...it is nice and short. I think it's readable, but others may disagree.

check_funcs = ["isalnum",
                  "isalpha",
                  "isdigit",
                  "islower",
                  "isupper",
                  ]

for func in check_funcs:
    print(any(getattr(char, func)() for char in string))

That could be your whole main() function if you wrote one. Or if you want to keep a separate function for printing you can do:

result_list = []
for func in check_funcs:
    result_list.append(any(getattr(char, func)() for char in string))
return result_list

or of course you could use a monster list comprehension:

   return [any(getattr(char, func)() for char in string) for func in check_funcs]

But I am not sure any of these versions are more readable than what you've got already.

Add another function to simplify your list comprehension

You can make your code more readable by creating a function to simplify your list comprehension. By defining an additional function like so :

def func_on_str(string, func):
    return any(fun(char) for char in s)

you can rewrite the return statement of check_string more clearly:

return [func_on_str(string, func) for func in check_funcs]

(you can see I agree with @SuperBiasedMan that your variable names could be clearer)

Use getattr() and the code gets shorter but not necessarily more readable

If you want to run member functions, here's what you can do ignoring SRP...it is nice and short. I think it's readable, but others may disagree.

check_funcs = ["isalnum",
                  "isalpha",
                  "isdigit",
                  "islower",
                  "isupper",
                  ]

for func in check_funcs:
    print(any(getattr(char, func)() for char in string))

That could be your whole main() function if you wrote one. Or if you want to keep a separate function for printing you can do:

result_list = []
for func in check_funcs:
    result_list.append(any(getattr(char, func)() for char in string))
return result_list

or of course you could use a monster list comprehension:

   return [any(getattr(char, func)() for char in string) for func in check_funcs]

But I am not sure any of these versions are more readable than what you've got already.

Bounty Awarded with 50 reputation awarded by Curt F.
Source Link
sunny
  • 1.9k
  • 1
  • 13
  • 29

Add another function to simplify your list comprehension

You can make your code more readable by creating a function to simplify your list comprehension. By defining an additional function like so :

def func_on_str(string, func):
    return any(fun(char) for char in s)

you can rewrite the return statement of check_string more clearly:

return [func_on_str(string, func) for func in check_funcs]

(you can see I agree with @SuperBiasedMan that your variable names could be clearer)

Use getattr() and the code gets shorter but not necessarily more readable

If you want to run member functions, here's what you can do ignoring SRP...it is nice and short. I think it's readable, but others may disagree.

check_funcs = ["isalnum",
                  "isalpha",
                  "isdigit",
                  "islower",
                  "isupper",
                  ]

for func in check_funcs:
    print(any(getattr(char, func)() for char in string))

That could be your whole main() function if you wrote one. Or if you want to keep a separate function for printing you can do:

result_list = []
for func in check_funcs:
    result_list.append(any(getattr(char, func)() for char in string))
return result_list

or of course you could use a monster list comprehension:

   return [any(getattr(char, func)() for char in string) for func in check_funcs]

But I am not sure any of these versions are more readable than what you've got already.