The attributes are callables and not properties.
In your example you are not calling the functions.
This example checks everything.
So when you see constructs like:
But Python doesn't have this construct.
Pythonists using as replacement a mapping.
The keys and values of a dict can also be callables (functions or methods).
The benefit is, that you have as first your data structure and afterwards
the code, which is executing the logic. Logic is spitted from data.
To preserve the Order, I use collections.OrderedDict.
In Python 3.7 they guarantee preserving the order in dicts as a
language feature and no longer as implementation detail.
In your example you are not calling the functions.
This example checks everything.
def pathftn(path): mapping = collections.OrderedDict([ ('is_symlink', 'l'), ('is_block_device', 'b'), ('is_char_device', 'c'), ('is_dir', 'd'), ('is_file', 'f'), ('is_socket', 's'), ('is_fifo', 'p'), ]) return ''.join( value if getattr(path, func)() else '_' for func, value in mapping.items() )Same function which does the same like yours:
def pathftn(path, unknown='?'): mapping = collections.OrderedDict([ ('is_symlink', 'l'), ('is_block_device', 'b'), ('is_char_device', 'c'), ('is_dir', 'd'), ('is_file', 'f'), ('is_socket', 's'), ('is_fifo', 'p'), ]) for func, value in mapping.items(): if getattr(path, func)(): return value else: return unknownThe mapping can be outside of the function.
So when you see constructs like:
if foo: x=10 elif bar: x=20 elif baz: x=30 else: x=-1You better use a switch statement like in C.
But Python doesn't have this construct.
Pythonists using as replacement a mapping.
The keys and values of a dict can also be callables (functions or methods).
The benefit is, that you have as first your data structure and afterwards
the code, which is executing the logic. Logic is spitted from data.
To preserve the Order, I use collections.OrderedDict.
In Python 3.7 they guarantee preserving the order in dicts as a
language feature and no longer as implementation detail.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!