Reputation: 1544
Which one of these two method invocations is considered most pythonic?
some_method(that_has, very_many, aurguments=None, of_different=None,
kinds=u'', and_importance=None, spanning=u'multple lines'):
or
method_args = {
u'first_arg' : that_has,
u'second_arg' : very_many,
u'arguments' : None,
u'of_different' : None,
u'kinds' : u'',
u'and_importance' : None,
u'spanning' : u'multiple lines'
}
some_method(**method_args)
Personally I prefer the second when the first spans more than 2 lines and the first way if it fits in one line, for two lines I am not quite certain.
Edit: The passed arguments might not be as short and static as in my example, more likely to be quite long names.
Upvotes: 2
Views: 104
Reputation: 8607
Whatever is more readable.
Once you reach a certain level of complexity for a function's arguments, it can be worthwhile to actually define a class which encapsulates those arguments:
args = SomeMethodArgs() # custom args class for the some_method func
args.x = 2
args.y = "a string"
args.z = None
args.items = [a, b, c]
some_method(args)
This has the benefit that you can modify the number of arguments or the signature later on in one place with minimal collateral effect.
Upvotes: 0
Reputation: 117661
If it's just a static call like that, the above is more Pythonic. If you want (and some people actually do this) you can align it like this:
some_method(
that_has,
very_many,
arguments=None,
of_different=None,
kinds=u'',
and_importance=None,
spanning=u'multple lines'
)
Also note that your second way doesn't even work - it only passes keyword arguments (to arguments that might not even be keywords).
Upvotes: 2